getc, getchar

Description

Reads a character from a stream (getc), or gets a character from stdin (getchar).

#include <stdio.h>

int getc( FILE *stream );

int getchar( void );

stream Current stream  

Remarks

The getc routine reads a single character from the stream position and increments the associated file pointer (if there is one) to point to the next character. The getchar routine is identical to getc(stdin).

The getc and getchar routines are similar to fgetc and _fgetchar, respectively, but are implemented both as macros and functions.

Return Value

Both 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 end-of-file occurred.

Compatibility

getc

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

getchar

Standards:ANSI, UNIX

16-Bit:DOS, QWIN

32-Bit:DOS32X

See Also

fgetc, _fgetchar, _getch, _getche, putc, putchar, ungetc

Example

/* GETC.C: This program uses getchar to read a single line of input

* from stdin, places this input in buffer, then terminates the

* string before printing it to the screen.

*/

#include <stdio.h>

void main( void )

{

char buffer[81];

int i, ch;

printf( "Enter a line: " );

/* Read in single line from "stdin": */

for( i = 0; (i < 80) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )

buffer[i] = ch;

/* Terminate string with null character: */

buffer[i] = '\0';

printf( "%s\n", buffer );

}

Output

Enter a line: This is a line of text.

This is a line of text.