Share via


getc, getwc, getchar, getwchar

Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).

intgetc(FILE*stream);

wint_tgetwc(FILE*stream);

intgetchar(void);

wint_tgetwchar(void);

Routine Required Header Compatibility
getc <stdio.h> ANSI, Win 95, Win NT
getwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
getchar <stdio.h> ANSI, Win 95, Win NT
getwchar <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns the character read. To indicate an read error or end-of-file condition, getc and getchar return EOF, and getwc and getwchar return WEOF. For getc and getchar, use ferror or feof to check for an error or for end of file.

Parameter

stream

Input stream

Remarks

Each of these routines reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character. In the case of getc and getwc, the file is associated with stream (see Choosing Between Functions and Macros). Routine-specific remarks follow.

Routine Remarks
getc Same as fgetc, but implemented as a function and as a macro.
getwc Wide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.
getchar Same as _fgetchar, but implemented as a function and as a macro.
getwchar Wide-character version of getchar. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_gettc getc getc getwc
_gettchar getchar getchar getwchar

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] = (char)ch;

   /* Terminate string with null character: */
   buffer[i] = '\0';
   printf( "%s\n", buffer );
}

Output

Enter a line: This is a test
This is a test

Stream I/O Routines

See Also   fgetc, _getch, putc, ungetc