# What are reading and writing characters in C language?

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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.

## Example

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>
main(void){
char ch;
printf (“enter chars, enter a period to stop\n”);
do{
ch = getche ();
if ( islower (ch) )
putchar (toupper (ch));
else
putchar (tolower (ch));
} while (ch! = ‘.’); /* use a period to stop */
return 0;
}

## Output

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

enter chars, enter a period to stop
tTuUtToOrRiIaAlLsS..

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.

Updated on 01-Sep-2021 12:50:41