C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4, j=-1, k=0, w, x, y, z;
    w = i || j || k;
    x = i && j && k;
    y = i || j &&k;
    z = i && j || k;
    printf("%d, %d, %d, %d\n", w, x, y, z);
    return 0;
}
1, 1, 1, 1
1, 1, 0, 1
1, 0, 0, 1
1, 0, 1, 1
Answer: Option
Explanation:

Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.

Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1

Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0

Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1

Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.

Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".

Discussion:
51 comments Page 2 of 6.

Purnima said:   1 decade ago
Step 1: i = 4, j = -1, k = 0
Take all values which are greater than or equal to zero as TRUE(T) and which are less than zero as FALSE(F).
Step 2: w = i || j || k
= 4 || -1 || 0
= (T || F) || T
= T || T = T
w = 1
Similarly we get all the expression. Check it out.

Prashant said:   10 years ago
Solve it in this manner.

For eg:

int i=4, j=-1, k=0, w, x, y, z;

y = I || j &&k;

z = I && j || k;

Sol: y = I || (j && k);

y = 4 || (-1 && 0);

y = 4 || (0) ; // gives 0 when -1 && 0.

y = 4; //gives true (1) when 4 or 0.



Solve to find z like above.

Wikiok said:   1 decade ago
As Rohit Maru said: && has higher priority than ||. So left-to-right associativity can not be used.
Step 4:
1st substep: "-1 && 0" --> False
2nd substep: "4 || False" --> True
So the answer is True, so it is "1".

Vicky kumar said:   1 decade ago
In step 4 the answer should be false but Wikiok said the right answer is true. It can't be happen because logical 'and' or 'or'.

Operator have left to right precedency.

But you are saying right to left how it is possible.

Please give me details.

Prashant said:   8 years ago
Step 4: y = i || j && k;
In step 4 put expression into brackets like : y = (i || j ) && k which will giev o/p as 0 which mean in real time && has a greater precedence than II so is the answer.

Udaya said:   1 decade ago
I think the Logic is like whenever they got OR operator they are not going for further expression as OR operator will return 1 of one operand is 1. And if the operator is && they were executing the whole expression.

Ashish said:   1 decade ago
The expression on the right hand side of && and || operator doesn't get evaluated if the left hand side determines the outcome.

Apply this logic.

4||(.) is always true. Thus w and y is 1.

Sid said:   8 years ago
@Prichita.

X=I && j && k;
So substitute values,
X=4 && -1&&0;
--> here 0 means false ....&& (Anding) any value with 0 become 0 only that is false only.

Bharadwaj said:   1 decade ago
In step 4, how the y value become true. i.e 4 || -1 && 0. How it results true.

4 || -1 is true and true && 0 means false. But it is returning true. Why?

Plz clarify my doubt

Saurav Agarwal said:   1 decade ago
In step:4 Since OR condition is satisfied it will not check AND condition. So the answer is true i.e, 1.

Simply check up to OR condition and if OR condition is satisfied leave the rest.


Post your comments here:

Your comments will be displayed after verification.