'C for Dummies' getchar() function not working

886 views Asked by At

I'm currently in the very early stages of learning C programming and am working my way through "Beginning Programming with C for Dummies" using Code::Blocks. The first activity in Chapter 7, Fetching characters with getchar(), asks us to copy the code exactly as it's presented in the book; see below:

#include <stdio.h>

int main()    
{
    int c;

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

The output I get is:

I'm waiting for a character: 

However according to the book, the output that I should be seeing is the character's ASCII code value. It then asks that I change the %c placeholder to %d to display the value, but still I get the same outcome as before. I could probably recite the code with my eyes closed I've checked it through that may times; I simply cannot see where I'm going wrong.

Am I right in thinking that the getchar() function isn't being recognized? Or that the code isn't being read after the first printf statement? Any guidance is welcome as I don't want to move on until I've understood the problem.

1

There are 1 answers

0
Jitendra On

Please enter any key, then 2nd printf will show the result. getchar() is expecting input from user, controller reach at 2nd line & waiting for input.