getchar declaration in c
The getchar
function in C is used to read a single character from the standard input (usually the keyboard) and return its ASCII value as an integer.
The declaration of getchar
in C is as follows:
int getchar(void);
The function takes no arguments and returns an int
value, which represents the ASCII value of the character read. If an error occurs or the end-of-file (EOF) is reached, the function returns the special value EOF
.
Here's an example of how getchar
can be used to read characters from the standard input:
#include <stdio.h>
int main() {
int c;
printf("Enter a character: ");
c = getchar();
printf("You entered: %c\n", c);
return 0;
}
In this example, the program prompts the user to enter a character, reads it using getchar
, and then prints the entered character using the %c
format specifier.
Please note that getchar
reads characters one at a time, including whitespace characters. If you want to read a string or a line of text, you will need to use other functions such as fgets
or scanf
.