C programming stdio.h function - int getchar(void)

www.igift‮i‬dea.com

The getchar() function is declared in the stdio.h header file and is used to read a single character from standard input, which is usually the keyboard.

The function signature is:

int getchar(void);

The function returns the character read as an unsigned char cast to an int or EOF on end of file or error.

Here's an example that uses getchar() to read and print the characters entered by the user until the user presses the Enter key:

#include <stdio.h>

int main() {
    int c;

    printf("Enter some characters (press Enter to finish):\n");

    while ((c = getchar()) != '\n' && c != EOF) {
        putchar(c);
    }

    printf("\n");

    return 0;
}

This program will continue to read and print characters until the user presses the Enter key, at which point the program exits.