Q. What is a standard library function to read 1 char at a time?
β
Correct Answer: (C)
getchar()
Explanation:
In C language, getchar() is a standard library function used to read a single character from the standard input (keyboard).
Usage of getchar()
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Reads a single character
printf("You entered: %c\n", ch);
return 0;
}
Why Not the Other Options?
- (A) putchar() β Outputs (prints) a single character, does not read.
- (B) printf() β Prints formatted output, does not read characters.
- (D) write() β A low-level system call, not a standard library function for character input.
Thus, (C) getchar() is the correct answer!