C - Read character from console using getc()

Introduction

getchar() is not a function. It's a macro defined in the stdio.h header file.

The real function to get characters from standard input is getc():

c = getc(stdin); 

In the code above, getc() reads from the standard input device, stdin, which is defined in the stdio.h header file.

The function returns an integer value, which is stored in variable c.

Demo

#include <stdio.h>

int main()//from  w  w w  .j  a v  a 2s.  com
{
    int c;

    printf("I'm waiting for a character: ");
    c = getc(stdin);
    printf("I waited for the '%c' character.\n",c);
    return(0);
}

Result

Related Topic