C++ Standard Library C++ STL Library

C++ <cstdio> - getchar() Function



The C++ <cstdio> getchar() function returns the next character from the standard input (stdin). It is equivalent to calling getc() function with stdin as argument.

Syntax

int getchar();

Parameters

No parameter is required.

Return Value

  • On success, the character read is returned.
  • If the failure has been caused due to end-of-file condition, returns EOF and sets the end-of-file indicator feof() on stdin.
  • If a read error occurs, the function returns EOF and sets the error indicator for the stream ferror() on stdin.

Example:

The example below shows the usage of getchar() function.

#include <cstdio>
 
int main (){

  int c;

  //prints characters till the point
  //it encounters the first '@' character
  printf("Enter text (Include '@' in the sentence to exit):");
  do {
    c = getchar();
    putchar(c);
  } while (c != '@');
  
  return 0;
}

If the following inputs are entered:

John
25
London
John@example.com

The output will be:

John
25
London
John@

❮ C++ <cstdio> Library