Namespaces
Variants

getchar

From cppreference.net
< c ‎ | io
Definido en el encabezado <stdio.h>
int getchar ( void ) ;

Lee el siguiente carácter desde stdin .

Equivalente a getc ( stdin ) .

Contenidos

Parámetros

(ninguno)

Valor de retorno

El carácter obtenido en caso de éxito o EOF en caso de fallo.

Si el fallo ha sido causado por una condición de fin de archivo, establece adicionalmente el indicador eof (ver feof() ) en stdin . Si el fallo ha sido causado por algún otro error, establece el indicador de error (ver ferror() ) en stdin .

Ejemplo

Demuestra getchar con verificación de errores

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    for (int ch; (ch = getchar()) != EOF;) // read/print "abcde" from stdin
        printf("%c", ch);
    // Test reason for reaching EOF.
    if (feof(stdin)) // if failure caused by end-of-file condition
        puts("End of file reached");
    else if (ferror(stdin)) // if failure caused by some other error
    {
        perror("getchar()");
        fprintf(stderr, "getchar() failed in file %s at line # %d\n",
                __FILE__, __LINE__ - 9);
        exit(EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}

Salida posible:

abcde
End of file reached

Referencias

  • Estándar C23 (ISO/IEC 9899:2024):
  • 7.21.7.6 La función getchar (p: TBD)
  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.21.7.6 La función getchar (p: TBD)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.21.7.6 La función getchar (p: 332)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.19.7.6 La función getchar (p: 298)
  • Estándar C89/C90 (ISO/IEC 9899:1990):
  • 4.9.7.6 La función getchar

Véase también

obtiene un carácter de un flujo de archivo
(función)
C++ documentation para getchar