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 CoverageThis class has only part of the semester dedicated to CYou already know Java, which has similar syntax You should be able to read the book quicklySimilarities of C to Java/* Comments */Variable declarationsIf / else statementsFor loopsWhile loopsFunction definitions (like methods)Main function starts programDifferences between C and JavaC does not have objectsThere are “struct”uresBut data are not tied to methodsC is a functional programming languageC allows pointer manipulationInput / Output with COutput with printf functionInput 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 Remarks5/9 = 0Primitive data types: int, float, char, short, long, doubleInteger 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 decimal5.0/9.0 = 0.555556Float has 32 bitsDouble has 64 bitsLong 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/OWe use “int” instead of “char” belowInput functions also return End-of-file (-1) and error conditionsUse 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 CharactersRemarks: 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 CountShort-circuit evaluation of || and &&nw++ at the beginning of a worduse state variable to indicate inside or outside a wordArraysCount #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;}FunctionsCall 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) VariablesAll variables declared so far are local to the functionsExternal or Global variables are accessible to all functionsThese are defined once outside of functionsMust be defined once more within each function which is going to use themPrevious program rewritten with external variablesNew Version with Externals/* Longest line
View Full Document