C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 16)
16.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x = 10, y = 20;
    if(!(!x) && x)
        printf("x = %d\n", x);
    else
        printf("y = %d\n", y);
    return 0;
}
y =20
x = 0
x = 10
x = 1
Answer: Option
Explanation:

The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.

Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.

Discussion:
13 comments Page 2 of 2.

Jitender Chhirang said:   1 decade ago
Can't understand what do we mean by !(!x) double not and how will we read this loop statement.

Ramya said:   1 decade ago
Here if (! (!x) &&x) means !(10) = 0 next among!, && ! has highest priority.

Therefore !(0) means 1, 1&&10 means again 1 because any non zero number gives true and true&&true is true if(true) means true therefore true block is executed and x = 10 is printed.

Pushpraj said:   8 years ago
Preference order of NOT operator is more than AND operator.


Post your comments here:

Your comments will be displayed after verification.