Previous: 4.1.2 Operations on Characters
Up: 4.1 A New Data Type: char
Next: 4.1.4 Strings vs Characters
Previous Page: 4.1.2 Operations on Characters
Next Page: 4.1.4 Strings vs Characters
We have already seen how to read and print characters using our usual I/O built in functions, scanf() and printf(); i.e. the %c conversion specifier. We have also included the header file stdio.h in all our programs, because it contains the definition for EOF, and declares prototypes for these formatted I/O routines. In addition, stdio.h contains two other useful routines, getchar() and putchar(), which are simpler to use than the formatted routines for character I/O. We use the term routine for getchar() and putchar() because they are actually macros defined in stdio.h which use more general functions available in the standard library. (Often routines that are macros are loosely referred to as functions since their use in a program can appear like a function call, so we will usually refer to getchar() and putchar() as functions).
The function getchar() reads a single character from the standard input and returns the character value as the value of the function, but to accommodate a possible negative value for EOF, the type of the value returned is int. (Recall, EOF may be either 0 or -1 depending on implementation). So we could use getchar() to read a character and assign the returned value to an integer variable:
int c;If, after executing this statement, c equals EOF, we have reached the end of the input file; otherwise, c is the ASCII value of the next character in the input stream.c = getchar();
While int type can be used to store the ASCII value of a character, programs can become confusing to read - we expect that the int data type is used for numeric integer data and that char data type is used for character data. The problem is that char type, depending on implementation, may or may not allow negative values. To resolve this, C allows us to explicitly declare a signed char data type for a variable, which can store negative values as well as positive ASCII values:
signed char c;An explicit signed char variable ensures that a character is stored in a character type object while allowing a possible negative value for EOF. The keyword signed is called a type qualifier.c = getchar();
A similar routine for character output is putchar(), which outputs its argument as a character to the standard output. Thus,
putchar(c);outputs the ASCII character whose value is in c to the standard output. The argument of putchar() is expected to be an integer; however, the variable c may be either char type or int type (ASCII value) since the value of a char type is really an integer ASCII value.
Since both getchar() and putchar() are macros
defined in stdio.h,
any program that uses these functions must include the stdio.h
header file in the program.
Let us rewrite our copy program using these new character I/O routines
instead of using scanf() and printf().
The new code is shown in Figure 4.5.
Characters are read until getchar() returns EOF. Each character read is printed using putchar(). Sample output is shown below.
The sample output shown here is for keyboard input so the effects of buffering the input is clearly seen: a line must be typed and entered before the characters become available in the input buffer for access by the program and then echoed to the screen.
Using getchar() and putchar() are simpler for character I/O because they do not require a format string as do scanf() and printf(). Also, scanf() stores a data item in an object whose address is given by its argument, whereas getchar() returns the value of the character read as its value. Both scanf() and getchar() return EOF as their value when they read an end of file marker in an input file.