0

Printf output

Hi guys Could you please assist me with following issue? #include <stdio.h> #include <stdlib.h> int main() { int score; printf("Please type in your score: %d\n", score); scanf("%d", &score); if (score >= 10) printf("WOW\n"); else printf("Go on\n"); return 0; } Why is the expression 0 for the first printf when I entry a figure?

17th Oct 2018, 11:15 AM
Codiak FOM
Codiak FOM - avatar
2 Réponses
+ 4
Using the uninitialized variable in expressions is considered undefined behaviour ¹ . It might be 0 on SoloLearn or a meaningless value like -123456789 on a workstation depends on the kind of variable that has been declared (the acceptable range of the garbage value) and the OS. So, you'd always want to make sure that all of the declared variables get initialized to a meaningful initial values (is always considered a good programming practice). _____ ¹ https://en.m.wikipedia.org/wiki/Undefined_behavior
17th Oct 2018, 11:41 AM
Babak
Babak - avatar
+ 2
you dont need to print score at the first printf, and it's not yet initialize
17th Oct 2018, 11:29 AM
Taste
Taste - avatar