What are reading and writing characters in C language?

In C programming language the reading and writing characters are as follows −

  • The simplest of the console I/O functions are getche (), which reads a character from the keyboard, and putchar (), which prints a character to the screen.

  • The getche () function works on until a key is pressed and then, returns its value. The key pressed is also echoed to the screen automatically.

  • The putchar () function will write its character argument to the screen at the current cursor position.

  • The declarations for getche () and putchar () are −

int getche (void);
int putchar (int c);
  • The header file for getche () and putchar () is in CONIO.H.


Here is an example which reads characters from the keyboard and prints them in reverse case. This means that the uppercase prints as lowercase and the lowercase prints as uppercase.

The program halts whenever a period is typed. The header file CTYPE.H is required by the islower() library function, which returns true if its argument is lowercase and false if it is not.

Following is the C program for the reading and writing characters

# include <stdio.h>
# include <conio.h>
# include <ctype.h>
   char ch;
   printf (“enter chars, enter a period to stop
”);    do{       ch = getche ();       if ( islower (ch) )          putchar (toupper (ch));       else          putchar (tolower (ch));    } while (ch! = ‘.’); /* use a period to stop */    return 0; }


When the above program is executed, it produces the following result −

enter chars, enter a period to stop

There are two important variations on getche(), which are as follows −

The first one is as follows −

  • The trouble with getchar() is that it buffers input until a carriage return is entered.

  • The getchar() function uses the STDIO.H header file.

The second one is as follows −

  • A second, more useful, variation on getche() is getch(), which operates precisely like getche () except that the character you type is not echoed to the screen. It uses the CONIO.H header.