Format
#include <stdio.h> int getc(FILE *stream); int getchar(void);
Language Level: ANSI, POSIX, XPG4
getc reads a single character from the current stream
position and advances the stream position to the next
character. getchar is identical to getc(stdin).
getc is equivalent to fgetc except that, if it is implemented as a macro, getc can evaluate stream more than once. Therefore, the stream argument to getc should not be an expression with side effects.
Return Value
getc and getchar return the character read. A return
value of EOF indicates an error or end-of-file condition. Use
ferror or feof to determine whether an error or an end-of-file
condition occurred.
Example
This example gets a line of input from the stdin stream.
You can also use getc(stdin) instead of getchar() in the for
statement to get a line of input from stdin.
#include <stdio.h> #define LINE 80
int main(void)
{
char buffer[LINE+1];
int i;
int ch;
printf( "Please enter string\n" );
/* Keep reading until either:
1. the length of LINE is exceeded or
2. the input character is EOF or
3. the input character is a new-line character
*/
for ( i = 0; ( i < LINE ) && (( ch = getchar()) != EOF) &&
( ch !='\n' ); ++i )
buffer[i] = ch;
buffer[i] = '\0'; /* a string should always end with '\0' ! */
printf( "The string is %s\n", buffer ); return 0;
/************************************************************
The output should be:
Please enter string
hello world
The string is hello world
************************************************************/
}
![]()
fgetc -- Read a Character
![]()
_fgetchar -- Read Single Character from stdin
![]()
_getch - _getche -- Read a Character from
Keyboard
putc - putchar -- Write a Character
ungetc -- Push Character onto Input
Stream
gets -- Read a Line
<stdio.h>