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

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

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

Abrar said:   1 decade ago
In step 4 how the value of y got 1?

In step 5 how the value of z got 1?

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

Then compute the result.
(1)

Monika said:   1 decade ago
Can anyone please tell the operation of && and ! operators.

Sathish said:   8 years ago
Anybody please clarify the doubt that how would the step 4 gives 1?

Richa said:   1 decade ago
Please explain the precedence and associativity of these operator.

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

Rohit Maru said:   1 decade ago
Priority of logical and (&&) is more than logical or (||).

Geetha said:   1 decade ago
In step 4 how that answer will come. Please give me explanation.

Priyanka Priya said:   9 years ago
Can anyone please explain the step 4?

How will it become true?


Post your comments here:

Your comments will be displayed after verification.