Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      getchar

      From cppreference.com
      <c‎ |io
       
       
      File input/output
      Types and objects
      Functions
      File access
      Unformatted input/output
      (C95)(C95)
      (C95)
      (C95)(C95)
      (C95)
      (C95)

      Formatted input
       
      Defined in header<stdio.h>
      int getchar(void);

      Reads the next character fromstdin.

      Equivalent togetc(stdin).

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      The obtained character on success orEOF on failure.

      If the failure has been caused by end-of-file condition, additionally sets theeof indicator (seefeof()) onstdin. If the failure has been caused by some other error, sets theerror indicator (seeferror()) onstdin.

      [edit]Example

      Demonstratesgetchar with error checking

      Run this code
      #include <stdio.h>#include <stdlib.h> int main(void){for(int ch;(ch= getchar())!=EOF;)// read/print "abcde" from stdinprintf("%c", ch); // Test reason for reaching EOF.if(feof(stdin))// if failure caused by end-of-file conditionputs("End of file reached");elseif(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);} returnEXIT_SUCCESS;}

      Possible output:

      abcdeEnd of file reached

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.21.7.6 The getchar function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.21.7.6 The getchar function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.21.7.6 The getchar function (p: 332)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.19.7.6 The getchar function (p: 298)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.9.7.6 The getchar function

      [edit]See also

      gets a character from a file stream
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/io/getchar&oldid=160129"

      [8]ページ先頭

      ©2009-2025 Movatter.jp