C getchar() Function | Read a Character from Standard Input

Introduction

The getchar() function in C is a standard library function that reads a single character from the standard input (stdin). It is part of the C standard library (stdio.h) and is commonly used for simple input operations.

Understanding getchar()

The getchar() function reads the next available character from the standard input (stdin) and returns it as an int. This function is useful for reading characters one at a time, handling input character by character, and for implementing features like character-based menus or simple text editors.

getchar() Function Syntax

The syntax for the getchar() function is as follows:

int getchar(void);

Parameters:

  • The getchar() function does not take any parameters.

Returns:

  • The function returns the next character from the standard input as an unsigned char cast to an int or EOF on end of file or error.

Examples

Reading a Single Character

To demonstrate how to use getchar() to read a single character from the standard input, we will write a simple program.

Example

#include <stdio.h>

int main() {
    int ch;

    // Prompt the user for input
    printf("Enter a character: ");

    // Read a single character from standard input
    ch = getchar();

    // Print the read character
    printf("You entered: %c\n", ch);

    return 0;
}

Output:

Enter a character: A
You entered: A

Using getchar() in a Loop

This example shows how to use getchar() in a loop to read multiple characters until a newline character is encountered.

Example

#include <stdio.h>

int main() {
    int ch;

    // Prompt the user for input
    printf("Enter text (press Enter to finish): ");

    // Read characters until newline is encountered
    while ((ch = getchar()) != '\n') {
        // Print each character
        printf("You entered: %c\n", ch);
    }

    return 0;
}

Output:

Enter text (press Enter to finish): Hello
You entered: H
You entered: e
You entered: l
You entered: l
You entered: o

Real-World Use Case

Implementing a Simple Menu

In real-world applications, the getchar() function can be used to implement simple menu-driven programs that read user choices one character at a time.

Example

#include <stdio.h>

void printMenu() {
    printf("Menu:\n");
    printf("1. Option 1\n");
    printf("2. Option 2\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
}

int main() {
    int choice;

    while (1) {
        // Print the menu
        printMenu();

        // Read the user's choice
        choice = getchar();

        // Clear the newline character from the input buffer
        while (getchar() != '\n');

        // Handle the user's choice
        switch (choice) {
            case '1':
                printf("You chose Option 1\n");
                break;
            case '2':
                printf("You chose Option 2\n");
                break;
            case '3':
                printf("Exiting...\n");
                return 0;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}

Output:

Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 1
You chose Option 1
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 3
Exiting...

Conclusion

The getchar() function is a simple and efficient way to read a single character from the standard input. It is often used in programs where character-by-character input is required, such as in text processing or interactive console applications.

Comments