arrow
arrow
arrow
​Which function is used to read a character from the console in C?​
Question

Which function is used to read a character from the console in C?

A.

putchar()

B.

getchar()

C.

scanf()

D.

gets()

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.

Free Tests

Free
Must Attempt

IBPS RRB PO Prelims 2026 Full Mock Test -01

languageIcon English
  • pdpQsnIcon80 Questions
  • pdpsheetsIcon80 Marks
  • timerIcon45 Mins
languageIcon English
Free
Must Attempt

IBPS RRB PO Prelims 2026 Reasoning Section Test- 01

languageIcon English
  • pdpQsnIcon40 Questions
  • pdpsheetsIcon40 Marks
  • timerIcon25 Mins
languageIcon English
Free
Must Attempt

IBPS RRB PO Prelims 2026 Quant Section Test- 01

languageIcon English
  • pdpQsnIcon40 Questions
  • pdpsheetsIcon40 Marks
  • timerIcon20 Mins
languageIcon English
test-prime-package

Access ‘Haryana CET Group C’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
415k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow