Skip to Main Content
C in a Nutshell
book

C in a Nutshell

by Peter Prinz, Tony Crawford
December 2005
Beginner to intermediate content levelBeginner to intermediate
618 pages
20h 19m
English
Content preview from C in a Nutshell

Name

getchar

Synopsis

Reads a character from the standard input stream

#include <stdio.h>
intgetchar( void );

The function call getchar() is equivalent to getc(stdin). Like getc(), getchar() may be implemented as a macro. As it has no arguments, however, unforeseen side effects are unlikely.

getchar() returns the character read. A return value of EOF indicates an error or an attempt to read past the end of the input stream. In these cases the function sets the error or end-of-file flag for stdin as appropriate.

Example

char file_name[256};
int answer;

/* ... */

fprintf( stderr, "Are you sure you want to replace the file \"%s\"?\n",
         file_name );
answer = tolower(getchar());
if ( answer != 'y' )
  exit( -1 );

See Also

fgetc(), fputc(), getch(), putc(), putchar(); the C99 functions to read and write wide characters: getwc(), fgetwc(), getwchar(), putwc(), fputwc(), and putwchar(), ungetc(), ungetwc()

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C in a Nutshell, 2nd Edition

C in a Nutshell, 2nd Edition

Peter Prinz, Tony Crawford
Head First C

Head First C

David Griffiths, Dawn Griffiths
Modern C

Modern C

Jens Gustedt

Publisher Resources

ISBN: 0596006977Errata Page