the Universe, and Everything using C. Print before scanf?

Sorry to ask such a noob question but, in Life, the Universe, and Everything using C.


int main()
int howmuch;
` while (howmuch != 42)
scanf("%d", &howmuch);
printf("%d\n", howmuch);
return 0;

Surely as long as I scan first then print it should be correct? I even scanned before the loop. Nothing worked until I scanned before the loop, printed in the loop then scanned again.
Just wondering why this code doesn’t work, again I’m quite new to all this.


What happens if the first value itself is 42. You have not initialized “howmuch” . So it may enter the loop first time and then print the first number scanned which is 42 , which it should not .
Otherwise also , this way 42 will always be printed whether 42 is the first number or not . You dont have to print 42 . You have to exit before that .

Also use \n instead of n in the printf statement otherwise it will give a WA.