180 likes | 252 Views
This tutorial covers basic character input and output in the C programming language, including examples of echo input, character counting, and line counting.
E N D
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
Char IO • Including the Standard Input/Output (stdio) library • #include <stdio.h> • Makes names of functions, variables, and macros visible • Read a single character • Read and returns a single character from the text stream “standard in” (stdin) • int getchar (); • Write a single character • Writes a single character c to “standard out” (stdout) • int putchar (int c);
Sample Code #include <stdio.h> int main () { int c; c = getchar (); putchar (c); return 0; }
Read Ten Chars #include <stdio.h> int main () { int c; int i; for (i=0; i<10; i++) { c = getchar(); putchar (c); } return 0; }
Infinite IO #include <stdio.h> int main () { int c; for (;;) { c = getchar (); putchar (c); } return 0; } // or a while version #include <stdio.h> int main () { int c; while (1) { c = getchar (); putchar (c); } return 0; }
Conditional IO #include <stdio.h> int main () { int c; c = getchar (); while (c != ‘a’) { c = getchar (); putchar (c); } return 0; }
EOF: Platform dependent: Ctrl-D on Linux Ctrl-Z on Windows Character Counting /* count characters in input */ #include <stdio.h> int main () { long nc = 0; while (getchar() != EOF) nc++; printf("%ld\n", nc); return 0; }
Line Counting /* count lines in input */ #include <stdio.h> int main() { long numLines = 0; int c; while((c=getchar()) != EOF) if (c == ‘\n’) numLines++; printf("%ld\n", numLines); return 0; }
Arrays • Thus far, we have seen: • Characters are just small integers (0-255) • More operations • ==, != • Control structures • Nested controls • Next, we consider how to count the number of ocurrences of characters ‘0’ to ‘9’
A First (Stupid) Try #include <stdio.h> int main() { long num0, num1, …, num9; num0 = num1 = … = num9 = 0; int c; while ((c=getchar()) != EOF) { if (c == ‘0’) num0++; else if (c == ‘1’) num1++; …; } printf (…); return 0; }
Arrays #include <stdio.h> int main() { long num[10]; int c, i; for (i=0; i<10; i++) num[i] = 0; while((c=getchar()) != EOF) if ((c >= ‘0’) && (c <= ‘9’)) num[c-’0’]++; return 0; }
Essence of Array • An array variable a is just the address of the 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
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 ? ? ? ? ?
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 0 ? ? ? ?
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 num 0 ? ? ? ?
An Example #include <stdio.h> voidfoo(long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } a num 999 ? ? ? ?
An Example #include <stdio.h> voidfoo(long a[]) { a[0] = 999; return; } int main() { long num[5]; num[0] = 0; foo (num); printf (“%ld\n”, num[0]); return 0; } num 999 ? ? ? ?