0

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.

16
  • 4
    After you see I'm waiting for a character:, do you type in a character? Commented Dec 21, 2016 at 5:38
  • 2
    try printf("I'm waiting for a character: ");fflush(stdout); Commented Dec 21, 2016 at 5:40
  • 1
    Notes: int main() is a deprecated signature. Use prototype-style declarations: int main(void) (for all functions, not just main). return is not a function, but a statement. Don't use parentheses around the argument, this can result in strange error messages for typos. The function-syle was used for K&R C, today it is more of a bad habit. Commented Dec 21, 2016 at 5:45
  • 1
    @Prometheus you are dead wrong. Commented Dec 21, 2016 at 6:51
  • 1
    @Prometheus stackoverflow.com/questions/18013167/… Commented Dec 21, 2016 at 7:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.