Single Character Functions

C++ supports many character-based input and output functions for reading and writing a character. These functions can read or write one character at a time. These functions are known as unformatted console I/O functions.

Getchar() and Putchar() Functions

The functions getchar() and putchar() are single character functions. The heeader file for these functions is stdio.h.

getchar()

The getchar() function reads a single character from the standard input device( keyboard). The getchar() waits for the character input until a charcter is typed at the keyboard.

For eg:

char c;

c=getchar();

...............

...............

Since getchar() us a stream I/O function, it is buffered ie. the character types by the user is not passed to the variable until the user hits the Enter of Return key.

putchar()

The function putchar() is used to send a single character to the standard output device. the character to be displayed on VDU screen is included as an argument to the putchar() function.

For eg:

char ans='y';

putchar(ans);

.......

.......

After the execution of the program, the character 'y' will be displayed on the screen. The putchar() function is also buffered.

Getc() and Putc() Functions

The functions getc() and putc() are also character-based functions. These functions are maily used with files. The header file for these functions is stdio.h. The function getc(stdin) is equivalent to the function getchar(). both will accept a character from the standard inpput device(keyboard). The difference is in terms of their working. The function getchar() reads a charater from the keyboard by default, whereas the argument stdin of function getc(stdin) makes it read from a file represented by a standard input device which is nothing but keyboard.

Similarly, the function putc(ch,stdout) is equivalent to the function putchar(). Both the functions send the character to the standard output device - the VDU screen.

Getche() and Putchch() Function

Getche() function reads directly from the console as soon as it is typed, without waiting for the Enter key to be pressed.The character 'e' of getche() function stands for echo ie. display the character on screen.

Putch() function directly writes a character on the console. It takes an argument. Eg: putch('\t') will take the cursor to the next tab position.

String-based Functions

gets()

The function gets() reads a string from the standard input device. It is buffered and therefore the Enter key is required to terminate the input.

puts()

The function puts() is used to send a string to the standard output device.

HOME LEARN C++ PREVIOUS NEXT