1 / 18

Character Input and Output

Character Input and Output. C and Data Structures Baojian Hua bjhua@ustc.edu.cn. Overview. We have talked about: Basic data types and C control structures This lecture: Basic character input and output Three examples: echo input directly to output character counting line counting.

Download Presentation

Character Input and Output

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. Character Input and Output C and Data Structures Baojian Hua bjhua@ustc.edu.cn

  2. Overview • We have talked about: • Basic data types and C control structures • This lecture: • Basic character input and output • Three examples: • echo input directly to output • character counting • line counting

  3. Char IO • Including the Standard Input/Output (stdio) library • #include <stdio.h> • Makes names of functions, variables, and macros available • Defining procedure main() • Starting point of the program, a standard boilerplate • Read a single character • Returns a single character from the text stream “standard in” (stdin) • char c = getchar(); • Write a single character • Writes a single character to “standard out” (stdout) • putchar (c);

  4. The Code #include <stdio.h> int main(void) { int c; c = getchar(); putchar(c); return 0; }

  5. self-increment operator Read Ten Chars #include <stdio.h> int main(void) { int c; int i; for (i=0; i<10; i++) { c = getchar(); putchar(c); } return 0; }

  6. Infinite IO #include <stdio.h> int main(void) { int c; int i; for ( ; ; ) { c = getchar(); putchar(c); } return 0; } // or a while version #include <stdio.h> int main(void) { int c; int i; while (1) { c = getchar(); putchar(c); } return 0; }

  7. Conditional IO #include <stdio.h> int main(void) { int c; int i; c = getchar(); while (c != ‘a’) { c = getchar(); putchar(c); } return 0; }

  8. Character Counting #include <stdio.h> /* count characters in input */ int main() { long nc; nc = 0; while (getchar() != EOF) { ++nc; } printf("%ld\n", nc); return 0; }

  9. Line Counting #include <stdio.h> /* count lines in input */ int main() { long numLines; numLines = 0; char c; while ((c=getchar()) != EOF) { if (c == ‘\n’) ++numLines; } printf("%ld\n", numLines); return 0; }

  10. Arrays • Thus far, we have seen: • Characters are just small integers (0-255) • More operations • ++i, i++, ==, != • Control structures • Nested controls • Next, we consider how to count the number of characters ‘0’ to ‘9’

  11. A First Try #include <stdio.h> int main() { long num0, num1, …, num9; num0 = num1 = … = num9 = 0; char c; while ((c=getchar()) != EOF) { if (c == ‘0’) ++num0; else if (c == ‘1’) ++num1; …; } printf("%ld\n", …); return 0; }

  12. Using Arrays #include <stdio.h> int main() { long num[10]; char c; int i; for (i=0; i<10; i++) num[i] = 0; while ((c=getchar()) != EOF) { if ((c >= ‘0’) && (c <= ‘9’)) ++num[c-’0’]; } return 0; }

  13. The Essence of Array • An array variable a is just a pointer pointing to the first array element a[0] • So when we pass an array to other functions, or we operate on the array variable, we are really operating on a pointer, not on array elements • More on this later

  14. An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; } ? ? num ? ? ?

  15. An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; } 0 ? num ? ? ?

  16. An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; } a 0 ? num ? ? ?

  17. An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; } a 999 ? num ? ? ?

  18. An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; } 999 ? num ? ? ?

More Related