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 1 of 6.

Sahana S Shenoy said:   7 years ago
All the expressions evaluates from left to right(in case of RHS and in case of w=expression, it is right to left that means expression is evaluated first then assigned to w) as the AND has the highest precedence over ||.

--->the number other than "zero" is always "true"

so w=T||T||F----->T||F
w=T
x=T&&T&&F---->T&&F
x=F
y=T||T&&F----->T||F
y=T
z=T&&T||F------>T||F
z=T.
(4)

Rutuja said:   4 years ago
Please explain why step 3 is false.

If T && F then it's T or F?
(2)

Suneeta mishra said:   3 years ago
And (&&)operator should be true for all conditions and in OR (||), at least one condition should be true.
(1)

Adi sha said:   4 years ago
Precedence is && then || (Associativity is right to left).
(1)

Anomie said:   5 years ago
@All.

Kindly read below logical operators with examples

&& Logical AND. True only if all operands are true.

If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.

|| Logical OR. True only if either one operand is true.
If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.

! Logical NOT. True only if the operand is 0

If c = 5 then, expression !(c==5) equals to 0.
(1)

MAHESH said:   8 years ago
Expressions are evaluated from RIGHT TO LEFT.

Then compute the result.
(1)

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.

Ahenkan Boakye-Ansah said:   1 decade ago
Step 5: Note this is not Bitwise Or and And. This is Logical OR and AND.

So if z = I && j || k; becomes z = 4 && -1 || 0; z = (1 && 1) || (0); z = (T && T) || (F).

z=1;

Or 4 and -1 are not 0, so it makes the statement true.

Or is 4=0? No.
Or is -1=0? No.
Or is 0=0? Yes.

So, z = (T && T) || (F).

z=1.

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.

Akash said:   1 decade ago
Make me understand how's 4 && -1 && 0 = 0.


Post your comments here:

Your comments will be displayed after verification.