# 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;
}
```

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.
Suneeta mishra said:
2 years ago

And (&&)operator should be true for all conditions and in OR (||), at least one condition should be true.

(1)

Adi sha said:
3 years ago

Precedence is && then || (Associativity is right to left).

Rutuja said:
3 years ago

Please explain why step 3 is false.

If T && F then it's T or F?

If T && F then it's T or F?

(2)

Anomie said:
4 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.

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)

Sahana S Shenoy said:
6 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.

--->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)

MAHESH said:
6 years ago

Expressions are evaluated from RIGHT TO LEFT.

Then compute the result.

Then compute the result.

(1)

Prashant said:
6 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.

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.

Dipak said:
6 years ago

Binary Number is a combination of 0 and 1.

So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.

We all know ANDing operation of two binary digit i.e.

1 1

1 0

-------------

1 0

Note: ANDing means multiplication of binary numbers.

we all know ORing operation of two binary digits i.e.

1 1

1 0

---------------

1 1

Note: ORing means addition of binary numbers.

So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.

We all know ANDing operation of two binary digit i.e.

1 1

1 0

-------------

1 0

Note: ANDing means multiplication of binary numbers.

we all know ORing operation of two binary digits i.e.

1 1

1 0

---------------

1 1

Note: ORing means addition of binary numbers.

Sid said:
7 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.

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.

Prichita said:
7 years ago

How did we get x=0?

Why is it false?

Why is it false?

Post your comments here:

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers