Character I/O: getchar and putchar in C Programming Language

The getchar function proved convenient when you wanted to read data from a single character at a time.You saw how you could develop a function called readLine to read an entire line of text from your terminal. This function repeatedly called getchar until a newline character was read.

There is an analogous function for writing data to the terminal a single character at a time. The name of this function is putchar.

A call to the putchar function is quite simple: The only argument it takes is the char-acter to be displayed. So, the call

putchar (c);

in which c is defined as type char, has the effect of displaying the character contained in c.

The call

putchar (‘\n’);

has the effect of displaying the newline character, which, as you know, causes the cursor to move to the beginning of the next line.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *