C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 10)
10.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a=100, b=200, c;
    c = (a == 100 || b > 200);
    printf("c=%d\n", c);
    return 0;
}
c=100
c=200
c=1
c=300
Answer: Option
Explanation:

Step 1: int a=100, b=200, c;
Step 2: c = (a == 100 || b > 200);
becomes c = (100 == 100 || 200 > 200);
becomes c = (TRUE || FALSE);
becomes c = (TRUE);(ie. c = 1)
Step 3: printf("c=%d\n", c); It prints the value of variable i=1
Hence the output of the program is '1'(one).

Discussion:
3 comments Page 1 of 1.

Eshant Sahu said:   1 decade ago
c = (a == 100 || b > 200);
Now , when the left side condition of the '||' operator is true, it will not check for right side condition,
So, c=(TRUE)
c=1;
Hence output of the program is '1'(one).

Kaushal 10dell said:   1 decade ago
Answer is right @Eshant.

But (i think).

According to hierarchy of operators '>' is to be calculated before '||'.

So after checking b>200 another check that is a==100 is performed.

In short both conditions are checked.

Anish Kumar said:   9 years ago
@Kaushal

Yes, you are right, both are a relational operator but > has higher precedence than ==.

Post your comments here:

Your comments will be displayed after verification.