Export (0) Print
Expand All
4 out of 12 rated this helpful - Rate this topic

getc, getwc, getchar, getwchar

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

int getc( 
   FILE *stream 
);
wint_t getwc( 
   FILE *stream 
);
int getchar( void );
wint_t getwchar( void );

Parameter

stream
Input stream.

Return Value

Returns the character read. To indicate a 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.

Remarks

Each routine 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. For 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

Requirements

Routine Required header Compatibility
getc <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
getwc <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
getchar <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
getwchar <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_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>

int main( void )
{
   char buffer[81];
   int i, ch;

   /* 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( "Input was: %s\n", buffer );
}

Input

This is a test

Output

Input was: This is a test

See Also

Stream I/O Routines | fgetc | _getch | putc | ungetc | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.