
Hi again! A short answer to this particular question is included in my answer to your previous question.
If you're still confused, you can try the following program and see what happens:
#include
int main() {
int age;
// get age input
printf("Enter age: ");
scanf("%d\n", &age);
// print age
printf("Age: %d", age);
return 0;
} You'll see that this program "hangs" after you've entered the age and pressed enter, i.e., it doesn't print the age after you've given the input.
The only way to make it print the age is to give another input and press enter. Here's my full output:
Enter age: 25
a
Age: 25Notice that I had to enter a (or any other non-whitespace input) to print the age.
Obviously, this is bad because the program appears to be hanging/freezing in the middle of the execution.
And as I've explained in my previous answer, this happens because you're telling scanf() to consume all whitespace characters and wait for non-whitespace character input.
That's what scanf("%d\n", &variable); does, and it's not something you want in your code.
Hope this helps! Let me know if you have any more questions.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer