C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
int main()
{
int x=12, y=7, z;
z = x!=4 || y == 2;
printf("z=%d\n", z);
return 0;
}
Answer: Option
Explanation:
Step 1: int x=12, y=7, z; here variable x, y and z are declared as an integer and variable x and y are initialized to 12, 7 respectively.
Step 2: z = x!=4 || y == 2;
becomes z = 12!=4 || 7 == 2;
then z = (condition true) || (condition false); Hence it returns 1. So the value of z=1.
Step 3: printf("z=%d\n", z); Hence the output of the program is "z=1".
Discussion:
12 comments Page 1 of 2.
Vinothini said:
2 decades ago
z = x!=4 || y == 2;
becomes z = 12!=4 || 7 == 2;
then z = TRUE || FALSE; Hence it returns TRUE. So the value of z=1.
how did you get z=1
becomes z = 12!=4 || 7 == 2;
then z = TRUE || FALSE; Hence it returns TRUE. So the value of z=1.
how did you get z=1
Rupali said:
1 decade ago
z = x!=4 || y == 2;
in this case when x!=4 is checked, it results TRUE & in the OR condition when the first condition becomes true the other condition is not checked.
Hence,y==2 will not be evaluated.
But ,overall value ,i.e. value of z=1 because the value of TRUE is 1.
in this case when x!=4 is checked, it results TRUE & in the OR condition when the first condition becomes true the other condition is not checked.
Hence,y==2 will not be evaluated.
But ,overall value ,i.e. value of z=1 because the value of TRUE is 1.
Sanjoy said:
1 decade ago
x! means=0.
So z=0.
So it could be false is not it?
So z=0.
So it could be false is not it?
Sivamoorthy said:
1 decade ago
The or operation (||) give one true means true.
So the result is true.
So we assign the value of z=0.
So the result is true.
So we assign the value of z=0.
Suguna said:
9 years ago
x!=0 which means 12!=0
So both the condition is false.
So, z=0.
So both the condition is false.
So, z=0.
Lata said:
9 years ago
The condition is true then how z = 1?
Roshnu said:
8 years ago
int main()
{
int x=12, y=7, z;
z = x!=4 || y = 2;
printf("z=%d\ty=%d\n", z,y);
return 0;
}
Can someone explain why the expression y=2 giving an error? It's not supposed to be executed.
{
int x=12, y=7, z;
z = x!=4 || y = 2;
printf("z=%d\ty=%d\n", z,y);
return 0;
}
Can someone explain why the expression y=2 giving an error? It's not supposed to be executed.
Vijay said:
7 years ago
@Suguna.
12!=0 is true 12=0 is false.
12!=0 is true 12=0 is false.
Surya said:
6 years ago
@Roshnu.
For the conditional statements u need to specify 2 '=' i.e y==2 not y=2.
For the conditional statements u need to specify 2 '=' i.e y==2 not y=2.
Anusha said:
6 years ago
How this x!=4?
12!=4
How anyone please explain?
12!=4
How anyone please explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers