Character Input/Output (I/O)
Character Input/Output (I/O) in C language is used to read and write single character data to and from the input/output devices. The standard I/O library in C provides functions for performing character I/O operations.
The following are some of the commonly used functions for character I/O in C:
getchar()
This function is used to read a single character from the standard input device (usually the keyboard) and returns the ASCII code of that character.
char ch = getchar(); // reads a single character from the keyboard
putchar()
This function is used to write a single character to the standard output device (usually the screen). It takes a single character as an argument and returns the ASCII code of the character written.
putchar('A'); // writes the character 'A' to the screen
gets()
This function is used to read a string of characters from the standard input device (usually the keyboard) and store it in an array. It continues reading until it encounters a newline character (\n) or the end-of-file (EOF) character.
char str[100];
gets(str); // reads a string of characters from the keyboard and stores it in str
puts()
This function is used to write a string of characters to the standard output device (usually the screen). It takes a null-terminated string as an argument and returns the number of characters written.
puts("Hello, world!"); // writes the string "Hello, world!" to the screen
scanf()
This function is used to read formatted input from the standard input device (usually the keyboard). It takes a format string as its first argument, which specifies the format of the input, and reads the input values accordingly.
char ch;
scanf("%c", &ch); // reads a single character from the keyboard and stores it in ch
printf()
This function is used to write formatted output to the standard output device (usually the screen). It takes a format string as its first argument, which specifies the format of the output, and writes the output values accordingly.
char ch = 'A';
printf("The character is %c", ch); // writes the string "The character is A" to the screen
Note that character I/O operations in C are usually buffered, which means that the characters are stored in a buffer before being read or written to the input/output device. The buffer is flushed (i.e., the characters are written to the device) either when the buffer is full, when a newline character (\n) is encountered, or when the program terminates.