C programming getchar stdio.h


In C programming the <stdio.h> getchar function gets the character from the console.The declaration of the function is given below.

Link : C getc stdio.h

int getchar(void);

Parameters:
void

Return type
int -Returns the next character from the input stream pointed to by stream.If the stream is at end of-file, the end-of-file indicator for the stream is set and ‘getchar’ returns EOF.If a read error occurs,the error indicator for the stream is set and ‘getchar’ returns ‘EOF’.

Note this function is same as ‘getc’ function with the argument ‘stdin’.
Code example

int c ;

printf(“Enter any character\n”);
c=getchar( );

printf(“\nc=%c” , c);

Output,

Enter any character
Q
c=Q

The program given below is same as above.

Code example

int c ;

printf(“Enter any character\n”);
c=getc( stdin );

printf(“\nc=%c” , c);

Output,

Enter any character
Q
c=Q

The function of ‘stdin’ stream is to allows reading character from the console screen/computer screen.


Related links

->C fgetc stdio.h