getchar() Function
The getchar()
function in C reads the next character from the standard input stream. It is commonly used for simple input operations, and is equivalent to calling getc(stdin)
. This function is particularly useful in situations where character-by-character input processing is required.
Syntax of getchar()
int getchar(void);
Parameters
No parameters.
Since there are no parameters, getchar()
automatically reads from the standard input stream. It returns the next character as an integer value, allowing it to accommodate the special EOF value for error handling or end-of-file conditions.
Return Value
On success, the character read is returned (promoted to an int). If the end-of-file is reached or if an error occurs, getchar()
returns EOF. In the case of EOF, the feof
indicator of stdin is set if it is the end-of-file, or the ferror
indicator is set if an error occurs.
Examples for getchar()
Example 1: Reading a Single Character
This example demonstrates the basic usage of getchar()
to read a single character from the user.
Program
#include <stdio.h>
int main() {
int ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n", ch);
return 0;
}
Explanation:
- The program prompts the user to enter a character.
getchar()
reads the next character from the standard input.- The read character is stored in the variable
ch
. - The program then prints the entered character.
Program Output:
Enter a character: A
You entered: A
Example 2: Reading Characters Until a Newline
This example shows how to use getchar()
in a loop to read and display characters until the newline character is encountered.
Program
#include <stdio.h>
int main() {
int ch;
printf("Enter text (press Enter to finish): ");
while ((ch = getchar()) != '\n' && ch != EOF) {
putchar(ch);
}
printf("\n");
return 0;
}
Explanation:
- The program prompts the user to enter text.
- A loop reads each character using
getchar()
until a newline or EOF is encountered. - Each character is immediately echoed to the output using
putchar()
. - After the loop, a newline is printed to complete the output.
Program Output:
Enter text (press Enter to finish): Hello World!
Hello World!
Example 3: Reading Characters Until End-of-File (EOF)
This example demonstrates how to use getchar()
to continuously read input until the end-of-file (EOF) is reached, which is useful when processing input streams with unknown lengths.
Program
#include <stdio.h>
int main() {
int ch;
printf("Enter text (press Ctrl+D to end):\n");
while ((ch = getchar()) != EOF) {
putchar(ch);
}
return 0;
}
Explanation:
- The program prompts the user to input text and indicates how to signal EOF (Ctrl+D on Unix/Linux, Ctrl+Z on Windows).
- A loop uses
getchar()
to read characters until EOF is encountered. - Each character is output using
putchar()
. - The loop terminates when no more input is available, ending the program.
Program Output:
Enter text (press Ctrl+D to end):
This is a test.
This is a test.
Another line.
Another line.