Home > Backend Development > C++ > body text

In C language, reading and writing characters refers to reading and writing character data through input and output functions.

WBOY
Release: 2023-09-23 08:33:02
forward
792 people have browsed it

In C language, reading and writing characters refers to reading and writing character data through input and output functions.

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);
Copy after login
  • getche() and putchar() The header file is in CONIO.H.

Example

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 (&ldquo;enter chars, enter a period to stop</p><p>&rdquo;);
   do{
      ch = getche ();
      if ( islower (ch) )
         putchar (toupper (ch));
      else
         putchar (tolower (ch));
   } while (ch! = &lsquo;.&rsquo;); /* use a period to stop */
   return 0;
}
Copy after login

Output

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

enter chars, enter a period to stop
tTuUtToOrRiIaAlLsS..
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!