Program to take character as input in C Programming.
C
#include <stdio.h>
int main() {
char input;
printf("Enter a character: ");
scanf("%c", &input);
printf("You entered: %c\n", input);
return 0;
}
Explanation:
- The #include <stdio.h> line includes the standard input/output library, which provides functions like printf() and scanf().
- The main() function is the entry point of the program.
- The variable input is declared as a character. It will store the user’s input.
- The printf() function displays the message “Enter a character: ” to prompt the user for input.
- The scanf() function with the %c format specifier is used to read a character from the user’s input. The & operator is used to pass the memory address of the input variable so that the value can be stored there.
- The printf() function displays the message “You entered: ” followed by the value stored in the input variable, using the %c format specifier.
- The return 0; statement indicates the successful completion of the main() function, and the program terminates.
C Output
Enter a character: E
You entered: E