getchar c

#include <stdio.h>

int main() {
    char c;

    printf("Enter a character: ");
    c = getchar();

    printf("You entered: ");
    putchar(c);

    return 0;
}

Explanation:

  • #include <stdio.h>: Includes the standard input-output library for basic input and output operations.

  • int main() { }: The main function where the program execution starts.

  • char c;: Declares a variable c of type char to store a single character.

  • printf("Enter a character: ");: Displays a message prompting the user to enter a character.

  • c = getchar();: Uses getchar() to read a single character from the standard input (keyboard) and assigns it to the variable c.

  • printf("You entered: ");: Displays a message indicating what the user entered.

  • putchar(c);: Outputs the character stored in the variable c to the standard output (console).

  • return 0;: Indicates successful completion of the main() function and the program terminates with a status code of 0.