Syntax
#include <stdio.h> int getc(FILE *stream); int getchar(void);Description
getc reads a single byte from the current stream position and advances the stream position to the next byte. 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. Therfore, the stream argument to getc should not be an expression with side effects.
getc and getchar return the value 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.
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 newline 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 similar to: Please enter string hello world The string is hello world ****************************************************************************/ }Related Information