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 3 of 6.
Mukesh Modi said:
1 decade ago
Everyone have doubt in step 4 and step 5.
Here I am clearing your doubt if I am not wrong.
Because of precedence order && got 11 number and || 12 and their associativity is L to R.
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1.
when I compiled -1 && 0 it returns T. so i|| T then answer is T.
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
i&&j that is 4&&-1 returns T. so T || k then answer is T.
so I think any -ve number(-1) is treated as T.
Here I am clearing your doubt if I am not wrong.
Because of precedence order && got 11 number and || 12 and their associativity is L to R.
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1.
when I compiled -1 && 0 it returns T. so i|| T then answer is T.
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
i&&j that is 4&&-1 returns T. so T || k then answer is T.
so I think any -ve number(-1) is treated as T.
Rajesh said:
1 decade ago
@Muesh modi.
In step 4: When compiled as per your logic y=4||-1&&0 then first -1&&0 returns false.
And then 4||false becomes t||f returns true.
In step 4: When compiled as per your logic y=4||-1&&0 then first -1&&0 returns false.
And then 4||false becomes t||f returns true.
Neha said:
1 decade ago
According to this program they have considered only from left to right. There is no logic of precedence.
Mayank said:
1 decade ago
Y = T||T && F/*&& HAS HIGHER PRIORITY THEN ||*/.
Y = T||F.
Y = 1;
Y = T||F.
Y = 1;
Megha salunkhe said:
1 decade ago
Please tell how step 4 got the result 1? How it is implemented?
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.
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.
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.
Apply this logic.
4||(.) is always true. Thus w and y is 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.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers