In the C programming language, the functions for reading and writing characters are as follows −
The simplest console input and output function is getche (), which reads a character from the keyboard, and putchar(), which prints a character to the screen.
The getche() function waits until a key is pressed and returns its value. The pressed keys are also automatically displayed on the screen.
The putchar() function writes its character parameters to the screen at the current cursor position.
The declarations of getche() and putchar() are as follows −
int getche (void); int putchar (int c);
getche() and putchar() The header file is in CONIO.H.
Below is an example that reads characters from the keyboard and prints them with reverse case. This means that uppercase letters print as lowercase letters and lowercase letters print as uppercase letters.
When you type a period, the program stops running. The islower() library function requires the header file CTYPE.H. This function returns true if its parameter is a lowercase letter, otherwise it returns false.
The following is an example of a C program 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</p><p>”); 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 results −
enter chars, enter a period to stop tTuUtToOrRiIaAlLsS..
There are two important getche() variants as follows −
The first variant is as follows −
getchar( ) is that it buffers the input until a carriage return is entered.
The getchar() function uses the STDIO.H header file.
The second variant is as follows −
The second more useful variant of getche() is getch(), which The operation is exactly the same as getche(), except that the characters you enter will not be displayed on the screen. It uses the CONIO.H header file.
The above is the detailed content of In C language, reading and writing characters refers to reading and writing character data through input and output functions.. For more information, please follow other related articles on the PHP Chinese website!