Outline Symbolic Constants Character Input and Output if … else switch…case
Symbol constants #include #define LOWER 0 #define UPPER 300 #define STEP 20 /* print F-S table */ main() { int fahr; for(fahr=LOWER; fahr <= UPPER; fahr = fahr+STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } Symbol name (Meaningful, easy to read) Symbol value Replace this symbol name at compile time
Character I/O A text stream is a sequence of characters getchar() getch() getche() getc() putchar(c) putch(c,stdout) putc(c,stdout) I/O devices are also taken as files 輸入 stdin 輸出 stdout
Example: File copying #include /* echo, version 1 */ main() { int c; c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); } not equal to End Of File A constant defined in stdio.h NOT the same as any char values
Example: File copying EOF Print the value of EOF End of file OSKeyboard termination signal(ctrl-Z) #include main() { printf("EOF = %d\n", EOF); }
Example: File copying Assignment c= getchar(); Assignment is an expression and has a value, which is the value of the left hand side after assignment. #include main() { int c; while( (c=getchar()) != EOF ){ putchar(c); } Precedence of = and != Textbook p. 5-25
Example: Character counting #include main() { long nc; /* number of character */ nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc); } Good naming Convention For variables nc = nc+1; 32-bit integer
Example : Character counting 2 #include main() { double nc; for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc); } null statement Increase range
Outline Symbolic Constants Character Input and Output if … else switch … case
Example: Line counting #include /* count lines */ main() { int c, nl; nl = 0; while( (c=getchar()) != EOF) if(c == '\n') ++nl; printf("%d\n", nl); } Test condition is equal to 條件測試 character constant ASCII value of input char. “ \n ” ?
if statement if( expression ){ statement 1; } else{ statement 2; } Test YES Statement 1 NO if statement 3; Statement 2 Statement 3 else
if-else: some notes if (expression) Nested if Else is associating with the closest previous else-less if if (expression != 0) if (n > 0) if (a > b) z = a; else z = b; if (n > 0){ if (a > b) z = a; } else z = b; if (n > 0) if (a > b) z = a; else z = b;
Example 李伯伯 example 1.1-3
Outline Symbolic Constants Character Input and Output if … else switch … case
Switch Multi-way decision switch(expression){ case const-expr : statements … default: statements } optional integer-valued constant * All case expressions must be different
Switch Test expression switch case expr1 statements 1 expr2 case … default statements N statements 2 break; (fall-through)
#include main() { int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for(i=0; i<10; i++) ndigit[i] = 0; while( (c=getchar()) != EOF){ switch(c){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; break; default: nother++; break; } printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } Not necessary, but good
printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } *note: fall-throughs are necessary when there are Multiple labels for a single computation