getchar() Function

The getchar() function in C reads the next character from the standard input stream. It is commonly used for simple input operations, and is equivalent to calling getc(stdin). This function is particularly useful in situations where character-by-character input processing is required.


Syntax of getchar()

</>
Copy
int getchar(void);

Parameters

No parameters.

Since there are no parameters, getchar() automatically reads from the standard input stream. It returns the next character as an integer value, allowing it to accommodate the special EOF value for error handling or end-of-file conditions.

Return Value

On success, the character read is returned (promoted to an int). If the end-of-file is reached or if an error occurs, getchar() returns EOF. In the case of EOF, the feof indicator of stdin is set if it is the end-of-file, or the ferror indicator is set if an error occurs.


Examples for getchar()

Example 1: Reading a Single Character

This example demonstrates the basic usage of getchar() to read a single character from the user.

Program

</>
Copy
#include <stdio.h>

int main() {
    int ch;
    
    printf("Enter a character: ");
    ch = getchar();
    
    printf("You entered: %c\n", ch);
    return 0;
}

Explanation:

  1. The program prompts the user to enter a character.
  2. getchar() reads the next character from the standard input.
  3. The read character is stored in the variable ch.
  4. The program then prints the entered character.

Program Output:

Enter a character: A
You entered: A

Example 2: Reading Characters Until a Newline

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

Program

</>
Copy
#include <stdio.h>

int main() {
    int ch;
    
    printf("Enter text (press Enter to finish): ");
    while ((ch = getchar()) != '\n' && ch != EOF) {
        putchar(ch);
    }
    printf("\n");
    return 0;
}

Explanation:

  1. The program prompts the user to enter text.
  2. A loop reads each character using getchar() until a newline or EOF is encountered.
  3. Each character is immediately echoed to the output using putchar().
  4. After the loop, a newline is printed to complete the output.

Program Output:

Enter text (press Enter to finish): Hello World!
Hello World!

Example 3: Reading Characters Until End-of-File (EOF)

This example demonstrates how to use getchar() to continuously read input until the end-of-file (EOF) is reached, which is useful when processing input streams with unknown lengths.

Program

</>
Copy
#include <stdio.h>

int main() {
    int ch;
    
    printf("Enter text (press Ctrl+D to end):\n");
    while ((ch = getchar()) != EOF) {
        putchar(ch);
    }
    
    return 0;
}

Explanation:

  1. The program prompts the user to input text and indicates how to signal EOF (Ctrl+D on Unix/Linux, Ctrl+Z on Windows).
  2. A loop uses getchar() to read characters until EOF is encountered.
  3. Each character is output using putchar().
  4. The loop terminates when no more input is available, ending the program.

Program Output:

Enter text (press Ctrl+D to end):
This is a test.
This is a test.
Another line.
Another line.