| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <stdio.h> | ||
int getchar(void); | ||
Reads the next character fromstdin.
Contents |
(none)
The obtained character on success orEOF on failure.
If the failure has been caused by end-of-file condition, additionally sets theeof indicator (seefeof()) onstdin. If the failure has been caused by some other error, sets theerror indicator (seeferror()) onstdin.
Demonstratesgetchar with error checking
#include <stdio.h>#include <stdlib.h> int main(void){for(int ch;(ch= getchar())!=EOF;)// read/print "abcde" from stdinprintf("%c", ch); // Test reason for reaching EOF.if(feof(stdin))// if failure caused by end-of-file conditionputs("End of file reached");elseif(ferror(stdin))// if failure caused by some other error{perror("getchar()");fprintf(stderr,"getchar() failed in file %s at line # %d\n", __FILE__, __LINE__-9);exit(EXIT_FAILURE);} returnEXIT_SUCCESS;}
Possible output:
abcdeEnd of file reached
| gets a character from a file stream (function)[edit] | |
C++ documentation forgetchar | |