hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    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.

    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
    192k+ students have already unlocked exclusive benefits with Test Prime!

    Free Tests

    Free
    Must Attempt

    IB ACIO Grade-II Executive (Held on 17 Jan 2024 S1)

    languageIcon English
    • pdpQsnIcon100 Questions
    • pdpsheetsIcon100 Marks
    • timerIcon60 Mins
    languageIcon English
    Free
    Must Attempt

    IB ACIO Grade II Full Mock Test 1

    languageIcon English
    • pdpQsnIcon100 Questions
    • pdpsheetsIcon100 Marks
    • timerIcon60 Mins
    languageIcon English
    Free
    Must Attempt

    General Studies Section Test 1

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon8 Mins
    languageIcon English