Friday, July 10, 2009

Character level I/O in C Programming Language

Character level I/O

C provides (through its libraries) a variety of I/O routines. At the character level, getchar() reads one character at a time from stdin, while putchar() writes one character at a time to stdout. For example, consider


#include <>

void main()
{
int i, nc;

nc = 0;
i = getchar();
while (i != EOF) {
nc = nc + 1;
i = getchar();
}
printf("Number of characters in file = %d\n", nc);
}


This program counts the number of characters in the input stream (e.g. in a file piped into it at execution time). The code reads characters (whatever they may be) from stdin (the keyboard), uses stdout (the X-terminal you run from) for output, and writes error messages to stderr (usually also your X-terminal). These streams are always defined at run time. EOF is a special return value, defined in stdio.h, returned by getchar() when it encounters an end-of-file marker when reading. Its value is computer dependent, but the C compiler hides this fact from the user by defining the variable EOF. Thus the program reads characters from stdin and keeps adding to the counter nc, until it encounters the ``end of file''.

An experienced C programmer would probably code this example as:



#include <>

void main()
{
int c, nc = 0;

while ( (c = getchar()) != EOF ) nc++;

printf("Number of characters in file = %d\n", nc);
}


C allows great brevity of expression, usually at the expense of readability!

The () in the statement (c = getchar()) says to execute the call to getchar() and assign the result to c before comparing it to EOF; the brackets are necessary here. Recall that nc++ (and, in fact, also ++nc) is another way of writing nc = nc + 1. (The difference between the prefix and postfix notation is that in ++nc, nc is incremented before it is used, while in nc++, nc is used before it is incremented. In this particular example, either would do.) This notation is more compact (not always an advantage, mind you), and it is often more efficiently coded by the compiler.

The UNIX command wc counts the characters, words and lines in a file. The program above can be considered as your own wc. Let's add a counter for the lines.



#include <>

void main()
{
int c, nc = 0, nl = 0;

while ( (c = getchar()) != EOF )
{
nc++;
if (c == '\n') nl++;
}

printf("Number of characters = %d, number of lines = %d\n",
nc, nl);
}

No comments:

Blog List