Explain the reason for the output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Explain the reason for the output

https://code.sololearn.com/cZv30k31KH3q/?ref=app

15th Aug 2019, 5:10 AM
Brutual bro
Brutual bro - avatar
3 Answers
+ 6
x is a global variable. All global variables are initialized with 0 by default. Now inside if statement if(x) which is equal to if(0) is evaluated as if(false) so the compiler moves to the else part of the if statement. 0 is considered as false 1 is considered as true
15th Aug 2019, 5:28 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
Global variables are initialized to the type's default value. In this case since it is an int variable its value is defaulted to zero. Your condition `if(x)` checks whether <x> is non zero, and given its value is zero (boolean: false) the `else` block is executed rather. (Edit) I didn't see an answer when I first open this thread 😁 Hth, cmiiw
15th Aug 2019, 5:29 AM
Ipang
+ 3
X is global variable and by default X is initialized to 0. So, always else part is executed. By initializing X with non-zero value, you can get value in if part. Hope this helps...!!!
15th Aug 2019, 5:38 AM
Kuri
Kuri - avatar