3.1. The getchar() and putchar() functions


The standard library provides several functions for reading or writing one character at a time, of which getchar and putchar are the simpliest. The int getchar(void) function reads the next available character from the screen and returns it as an integer. The int putchar(int c) function puts the passed character on the screen and returns the same character.

Each time it is called, getchar reads the next input character from a text stream and returns that as its value. That is, after         c = getchar();           variable c contains the next character of input which usually comes from keyboard.

The function putchar prints a character each time it is called:      putchar(c);   usually on the screen.

Calls to putchar and printf may be interleaved, the output will appear in the order in which the calls are made.


#include <stdio.h>
int main( ) 
   int c;
   printf( "Enter a value :");
   c = getchar( );
   printf( "\nYou entered: ");
   return 0;/* because main() is int type */

Try this code to see how it works and do in the same way with the other examples later.

A program that copies its input to its output one character at a time, first the pseudocode (like we learned in course Introduction to programming):

1.read a character

2.while (character is not end-of-file indicator EOF)

3.output the character just read

4.read a character

Next same into C:

/*copy input to output*/
int symbol;
printf( “Enter a value and press Enter:\n”);
symbol= getchar();/*the first character is read*/
while( symbol !=EOF)/*loop until the EOF is found, != means not equal to. If EOF doesn’t work, try ‘\n’ instead.*/
putchar(symbol);/*prints the character*/
symbol= getchar();/*next character is read*/

You may wonder why we use type int instead of char in variable symbol, while we are reading characters. An explanation really is needed and it is simple. The problem is distinguishing the end of the input from valid data. The solution is that getchar returns a distinctive value when there is no more input, a value that cannot be confused with any real character.

We must declare symbol to be a type big enough to hold any value that getchar returns. We can’t use char since symbol must be big enough to hold EOF in addition to any possible char.

EOF is  an integer defined in and its value is -1. In Cygwin and Visual studio its value is defined (-1) and therefore that EOF don’t work properly.

You can stop the program running pushing the buttons Ctrl and C simultaneously.

Same program in a shorter way:


/*copy input to output 2*/
int symbol;
printf( “Enter a value and press Enter:\n”);
while( (symbol= getchar()) != EOF)

NOTE! Now it is very important that parentheses are on the right place: (symbol= getchar()), because  program first reads the character and then assigns the value to the variable symbol, and after that it checks if the read character is not equal than  \n.  The precedence of != is higher than of =, which means that  in the absence of parentheses the relation test != would be done before the assignment = and it could not be a desirable outcome . So the statement
symbol = getchar() != EOF
is equivalent to
symbol = (getchar() != EOF)

NOTE! This chapter does not include exercises but this information will be tested in quizzes 3-4! It is very important that the student will make all examples shown in this chapter.