The standard C library provides several functions and macros for character 1/0. Here we consider the getchar and putchar macros. As these macros read or write a single character, they are typically used in a loop to read/write a sequence of characters.
A macro call is similar to a function call. Thus, it consists of a macro name followed by a comma-separated argument list enclosed in a pair of parentheses. If a macro does not require any arguments, the pair of parentheses must still be used.
The getchar macro is used to read a single character from the standard input stream, i. e., the keyboard. A call to get char takes the following form: getchar (). This macro waits until a key is pressed and then returns its value (after converting to integer). The value returned can be assigned to a variable of type char. However, note that this macro actually reads the data from the input buffer which is processed by program only when the Enter key is pressed. Thus, getchar does not return a value until the user presses the Enter key. As a result, it is not suitable for interactive programs. We can instead use the getch and getche functions which directly read the data from the keyboard.
The putchar macro is used to write a single character on the standard output stream (i.e., display). A call to this macro takes the following form: putchar (c), where c is an integer expression representing the character being written. Although this macro expects an argument of type int, we usually pass a character to it. The argument value (c) is converted to unsigned char and written to the standard output stream.
The getch and getche Functions
The getchar macro is not suitable in interactive environments due to the use of line buffering. The C standard library does not provide any facility that is guaranteed to provide an interactive character input. Hence, most C compilers provide alternative functions for use in such interactive environments. These include the getch and getche functions provided in <conio.h> (i. e., console I/O) header file. Like the getchar function, calls to these functions take the following forms: getch () and getche (). These functions are available in Turbo C/C++ as well as Dev-C++. In Visual C++, these functions arc named _getch and _getche.
The getch and getche functions wait for a key press and immediately return its value. The only difference between these functions is that the getche function echos (i. e., displays) the entered character on the screen, whereas the getch function does not echo it.
Illustrates the action of function getchar ()
#include<stdio.h> void main() { char ch; int count =0, i =0; char Str [33] ; clrscr(); printf("Write a short sentence:\n"); //The following code counts number of characters in string. while ( (ch = getchar ()) != '.') { Str[i] = ch; count++; i++; } Str[i] = '\0'; // append null character at end of string printf("The number of characters read are= %d\n", count); printf("You have written the following sentence.\n"); puts(Str); //display the string on std output device. }
As provided in the while expression the system stops reading the code on encountering the character ‘ . ‘ . Even ‘ . ‘ is not included in the string. Since reading is done character by character, this provides an opportunity to access an individual character. So, character counting is possible.