DOC PREVIEW
GSU CSC 3320 - chapterC

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45C ProgrammingA Modern ApproachK. N. King, C ProgrammingA Modern Approach,W. W. Norton & Company, 1996.Original Notes by Raj SunderramanConverted to presentation and updated by Michael WeeksNote About CoverageThis class has only part of the semester dedicated to CYou already know Java, which has similar syntax You should be able to read the book quicklySimilarities of C to Java/* Comments */Variable declarationsIf / else statementsFor loopsWhile loopsFunction definitions (like methods)Main function starts programDifferences between C and JavaC does not have objectsThere are “struct”uresBut data are not tied to methodsC is a functional programming languageC allows pointer manipulationInput / Output with COutput with printf functionInput with scanf functionHello, World#include <stdio.h>/* Standard I/O library *//* Function main with no arguments */int main () { /* call to printf function */ printf("Hello, World!\n"); /* return SUCCESS = 1 */ return 1; }% gcc -o hello hello.c % helloHello, World!%Celsius vs Fahrenheit table (in steps of 20F)C = (5/9)*(F-32); #include <stdio.h> int main() { int fahr, celsius, lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr - 32) / 9; printf("%d\t%d\n",fahr, celsius); fahr += step; } return 1; }Celsius vs Fahrenheit table Remarks5/9 = 0Primitive data types: int, float, char, short, long, doubleInteger arithmetic: 0F = 17C instead of 17.8C%d, %3d, %6d etc for formatting integers\n newline \t tabNew Version Using Float #include <stdio.h> int main() { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while (fahr <= upper) { celsius = (5.0 / 9.0) * (fahr - 32.0); printf("%3.0f %6.1f \n", fahr, celsius); fahr += step; } return 1; }New Version Using FloatRemarks%6.2f 6 wide; 2 after decimal5.0/9.0 = 0.555556Float has 32 bitsDouble has 64 bitsLong Double has 80 to 128 bits Depends on computerVersion 3 with “for” loop#include <stdio.h>int main() { int fahr; for (fahr=0; fahr <= 300; fahr += 20) printf("%3d %6.1f \n", fahr, (5.0 / 9.0) * (fahr – 32.0)); return 1;}Version 4 with Symbolic Constants#include <stdio.h>#define LOWER 0#define UPPER 300#define STEP 20int main() { int fahr; for (fahr=LOWER; fahr <= UPPER; fahr += STEP) printf("%3d %6.1f \n", fahr, (5.0 / 9.0) * (fahr - 32.0)); return 1;}Character I/OWe use “int” instead of “char” belowInput functions also return End-of-file (-1) and error conditionsUse feof() and ferror() to tell the difference int c; c = getchar(); putchar(c);File Copying #include <stdio.h> int main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } return 0; }File Copying (Simpler Version) #include <stdio.h> int main() { int c; c = getchar(); while ((c = getchar())!= EOF) putchar(c); return 0; }c= getchar() != 0 is equivalent to c = (getchar() != EOF)Results in c value of 0 (false) or 1 (true)Counting CharactersRemarks: nc++, ++nc, --nc, nc-- %ld for long integer #include <stdio.h> int main () { long nc = 0; while (getchar() != EOF) nc++; printf("%ld\n",nc); } #include <stdio.h> int main () { long nc; for (nc=0;getchar() != EOF;nc++); printf("%ld\n",nc); }Counting Lines #include <stdio.h> int main () { int c, nl=0; while ((c = getchar()) != EOF) if (c == '\n') nl++; printf("%d\n",nl); }Counting Words #include <stdio.h> #define IN 1 #define OUT 0 int main () { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') nl++; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n",nc, nw, nl); }Notes about Word CountShort-circuit evaluation of || and &&nw++ at the beginning of a worduse state variable to indicate inside or outside a wordArraysCount #occurrences of each digit, blank, tab, newline, other characters#include <stdio.h>int main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i=0; i<10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c > '0' && c < '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits = "); for (i=0; i<10; ++i) printf("%d ",ndigit[i]); printf(", white space = %d, other = %d\n",nwhite, nother);}Functions#include <stdio.h>int power(int, int); /* function prototype */int main() { int i; for (i = 0; i < 10; i++) printf("%d %d %d\n", i, power(2,i), power(-3,i));}int power(int base, int n) { int p; for (p = 1; n > 0; --n) p = p * base; return p;}FunctionsCall by value mechanism Change in n's value not reflected in main Using pointers, we can achieve Call by reference effect.Functions/* Character Arrays Read a set of text lines and print the longest*/#include <stdio.h>#define MAXLINE 1000int getline(char [],int);void copy(char [], char []);int main() { int len, max; char line[MAXLINE], longest[MAXLINE]; max = 0; while ((len = getline(line,MAXLINE)) > 0) if (len > max) { max = len; copy(longest,line); } if (max > 0) printf("%s",longest);}Getline and Copyint getline(char s[], int limit) { int c, i; for (i=0; i<(limit - 1) && (c=getchar())!=EOF && c != '\n'; i++) s[i] = c; if (c == '\n') { s[i] = c; i++; } s[i] = '\0'; return i;}void copy(char to[], char from[]) { int i=0; while ((to[i] = from[i]) != '\0') i++;}External (Global) VariablesAll variables declared so far are local to the functionsExternal or Global variables are accessible to all functionsThese are defined once outside of functionsMust be defined once more within each function which is going to use themPrevious program rewritten with external variablesNew Version with Externals/* Longest line


View Full Document

GSU CSC 3320 - chapterC

Download chapterC
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view chapterC and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view chapterC 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?