Use C getchar function to read character from standard input

Syntax

C getchar function has the following syntax.

int getchar(void);

C getchar function is from header file stdio.h.

Description

C getchar function read a character from stdin and returns EOF on error.

Read characters from stdin into the array s until the user presses ENTER

Example

Read character from console by using C getchar function


#include <stdio.h>
//w  w w.  ja  va 2 s  .  c  o m
int main(void)
{
  char s[256], *p;

  p = s;
  printf("input char:");
  while((*p++ = getchar())!= '\n');
  *p = '\0'; /* add null terminator */

  printf(s);

  return 0;
}




















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h