Export (0) Print
Expand All
div
eof
Expand Minimize

getchar, getwchar 

Read a character from standard input.


int getchar();
wint_t getwchar();

Returns the character read. To indicate a read error or end-of-file condition, getchar returns EOF, and getwchar returns WEOF. For getchar, use ferror or feof to check for an error or for end of file.

Each routine reads a single character from stdin and increments the associated file pointer to point to the next character. getchar is the same as _fgetchar, but it is implemented as a function and as a macro.

These functions lock the calling thread and are therefore thread-safe. For a non-locking version, see _getchar_nolock, _getwchar_nolock.

Generic-Text Routine Mappings
TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined

_gettchar

getchar

getchar

getwchar

Routine Required header Compatibility

getchar

<stdio.h>

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

getwchar

<stdio.h> or <wchar.h>

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// crt_getchar.c
// Use getchar to read a line from stdin.

#include <stdio.h>

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

    for (i = 0; (i < 80) && ((ch = getchar()) != EOF)
                         && (ch != '\n'); i++)
    {
        buffer[i] = (char) ch;
    }
       
    // Terminate string with a null character 
    buffer[i] = '\0';
    printf( "Input was: %s\n", buffer);
}

Input

This text

Output

Input was: This text

Community Additions

ADD
Show:
© 2014 Microsoft