1 / 19

Outline

Outline. Symbolic Constants Character Input and Output if … else switch…case. Symbol constants. Symbol name (Meaningful, easy to read). #include <stdio.h> #define LOWER 0 #define UPPER 300 #define STEP 20 /* print F-S table */ main() { int fahr;

Download Presentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline • Symbolic Constants • Character Input and Output • if … else • switch…case

  2. Symbol constants Symbol name (Meaningful, easy to read) #include <stdio.h> #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 value Replace this symbol name at compile time

  3. Character I/O • A text stream is a sequence of characters getchar() getch() getche() getc() putchar(c) putch(c,stdout) putc(c,stdout) 輸入 stdin I/O devices are also taken as files 輸出 stdout

  4. Example: File copying #include <stdio.h> /* echo, version 1 */ main() { int c; c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); } } End Of File not equal to A constant defined in stdio.h NOT the same as any char values

  5. Example: File copying End of file • EOF • Print the value of EOF OS Keyboard termination signal(ctrl-Z) #include <stdio.h> main() { printf("EOF = %d\n", EOF); }

  6. Example: File copyingAssignment • c= getchar(); • Assignment is an expression and has a value, which is the value of the left hand side after assignment. #include <stdio.h> main() { int c; while( (c=getchar()) != EOF ){ putchar(c); } } Precedence of = and != Textbook p. 5-25

  7. Example: Character counting #include <stdio.h> main() { long nc; /* number of character */ nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc); } Good naming Convention For variables 32-bit integer nc = nc+1;

  8. Example : Character counting 2 #include <stdio.h> main() { double nc; for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc); } Increase range null statement

  9. Outline • Symbolic Constants • Character Input and Output • if … else • switch … case

  10. Example: Line counting #include <stdio.h> /* count lines */ main() { int c, nl; nl = 0; while( (c=getchar()) != EOF) if(c == '\n') ++nl; printf("%d\n", nl); } “\n” ? 條件測試 Test condition character constant ASCII value of input char. is equal to

  11. if statement if if( expression ){ statement 1; } else{ statement 2; } YES Test Statement 1 NO else Statement 2 statement 3; Statement 3

  12. 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;

  13. Example • 李伯伯 example 1.1-3

  14. Outline • Symbolic Constants • Character Input and Output • if … else • switch … case

  15. Switch • Multi-way decision integer-valued constant switch(expression){ caseconst-expr : statements caseconst-expr : statements … default: statements } optional * All case expressions must be different

  16. switch Switch Test expression case expr1 statements 1 (fall-through) case expr2 statements 2 break; … default statements N

  17. #include <stdio.h> 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

  18. 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

More Related