Q. What is a standard library function to read 1 char at a time?

  • (A) putchar()
  • (B) printf()
  • (C) getchar()
  • (D) write()
πŸ’¬ Discuss
βœ… 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!

Explanation by: Mr. Dubey

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!

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
300
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Akash Lawaniya
Publisher
πŸ“ˆ
82%
Success Rate