I recently started taking a C course and I am having some difficulties running a simple I/O program:
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
scanf("%d\n", age);
printf("Your age is: %d", age);
return 0;
}
the problem is that the program does start running, but it never stops and it doesn't allow me to type in the age and doesn't even print out the "Please enter your age: " sentence. I have a gcc, g++ and a gdb installed and everything works fine if I remove the "scanf".
I would really appreciate some help! Thank you!
\n) in yourscanfformat string. It will almost never work as expected.stdoutis line buffered. That means the output is not flushed to your terminal unless a\nis printed orstdoutis flushed manually. As a result the program might be ready to take your input but you just cannot see any text asking you to do so. Try adding a\nat the end of yourprintfs. For a quick test you can just start typing your age without waiting for the prompt message.fflush(stdout)after eachprintfcall.