Reputation: 823
I came across a question in the book and it asked me to write the output of the following program.
#include<stdio.h>
int main()
{
int j=4;
( !j != 1 ? printf("\nWelcome") : printf("GooD Bye"));
return 0;
}
I am not able to understand basically how the Welcome is printed on running the program. Can anyone please explain with the help of hierarchy of operators and what values the compiler calculates according to the expression?
Upvotes: 1
Views: 313
Reputation: 106102
The line
( !j != 1 ? printf("\nWelcome") : printf("GooD Bye"));
is equivalent to
if(!j != 1)
printf("\nWelcome);
else
printf("Good Bye");
Here !j
evaluates to 0
therefore the condition !j != 1
will always be true and it will print Welcome
.
Upvotes: 7
Reputation: 1833
yeah the confusion of c!!
Ok in c !j evaluates to 0, since it is a number that is not 0 so 0 != 1 is true there for the true part of the ternary operation is executed and "welcome" is printed.
so to revaluate:
!4 = 0 //or any number
!0 = 1
Upvotes: 1