Correct option is B
getchar() is a standard library function in C that reads a single character from the standard input (console/keyboard). It waits for the user to input a character and press Enter, then returns the ASCII value of that character as an integer. This function is defined in the stdio.h header file and is the most direct way to read a single character from console input.
Important Key Points:
- Single Character Input: Reads exactly one character at a time.
- Return Type: Returns an int (not char) to handle EOF conditions.
- Standard Input: Reads from stdin (console by default).
- Header File: Requires #include <stdio.h>.
- Buffered Input: Input is buffered until Enter is pressed.
- EOF Handling: Returns EOF (-1) when end-of-file is reached.
- No Parameters: Takes no arguments - int getchar(void).
Knowledge Booster (Incorrect Options):
· putchar() is used for output, not input. It writes a single character to the standard output (console). It's the counterpart function for displaying characters.
· scanf() is used for formatted input and can read various data types including characters, but it's more complex and designed for reading formatted data with format specifiers like %c, %d, %s, etc.
· gets() is used to read an entire string/line from input, not a single character. Additionally, gets() is considered deprecated and unsafe due to buffer overflow vulnerabilities.