C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 16)
16.
What will be the output of the program?
#include<stdio.h>
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}
Answer: Option
Explanation:
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
Discussion:
13 comments Page 1 of 2.
Sundar said:
1 decade ago
The following program with output will help you understand this problem clearly.
//Output:
2 is Evaluated to True.
-2 is Evaluated to True.
0.2 is Evaluated to True.
0 - Evaluated to False.
#include<stdio.h>
int main()
{
if(2) printf("2 is Evaluated to True. \n");
if(-2) printf("-2 is Evaluated to True. \n");
if(0.2) printf("0.2 is Evaluated to True. \n\n");
if(!0) printf("0 - Evaluated to False. \n");
return 0;
}
//Output:
2 is Evaluated to True.
-2 is Evaluated to True.
0.2 is Evaluated to True.
0 - Evaluated to False.
Ramya said:
1 decade ago
Here if (! (!x) &&x) means !(10) = 0 next among!, && ! has highest priority.
Therefore !(0) means 1, 1&&10 means again 1 because any non zero number gives true and true&&true is true if(true) means true therefore true block is executed and x = 10 is printed.
Therefore !(0) means 1, 1&&10 means again 1 because any non zero number gives true and true&&true is true if(true) means true therefore true block is executed and x = 10 is printed.
Tufananand said:
1 decade ago
In if() condition, inside the parenthesis if we put any number like 1 2 3 etc. but not zero then in c language it is considered to be true.
Eg.
if(1)->true
if(5)->true
if(-7)->true etc.
But if(0)->false
Now compare this with above program. I hope you understand now.
Eg.
if(1)->true
if(5)->true
if(-7)->true etc.
But if(0)->false
Now compare this with above program. I hope you understand now.
Abhay said:
8 years ago
! (not) is a logical operator and it only gives output in 0 or 1. Don't confuse it with ~ (one's complement) operator which inverts the bits.
Try this on any compiler.
x=10;
y=!x;
a=!(!x);
z=~x;.
You will get y=0, a=1 and z=-11(if 2's complement signed representation is used).
Try this on any compiler.
x=10;
y=!x;
a=!(!x);
z=~x;.
You will get y=0, a=1 and z=-11(if 2's complement signed representation is used).
(1)
Aditya bhave said:
10 years ago
(!10) is 0?
It should be !(1010) that is 0101. And again !(0101) = 1010 = 10 in decimal.
So in final statement 10&&10.
i.e (1010 && 1010) = 1010 = 10 in decimal that's the answer.
It should be !(1010) that is 0101. And again !(0101) = 1010 = 10 in decimal.
So in final statement 10&&10.
i.e (1010 && 1010) = 1010 = 10 in decimal that's the answer.
(3)
Pratyush said:
1 decade ago
@Vikky. '!' it is a logical not operator.
It works like this:
!(0) -> 1.
!(any non zero no.) -> 0.
!!(0) -> 0.
!!(any non zero no.) -> 1.
It works like this:
!(0) -> 1.
!(any non zero no.) -> 0.
!!(0) -> 0.
!!(any non zero no.) -> 1.
Jitender Chhirang said:
1 decade ago
Can't understand what do we mean by !(!x) double not and how will we read this loop statement.
Pushpraj said:
8 years ago
Preference order of NOT operator is more than AND operator.
Vignesh R said:
1 decade ago
Some one please help I cannot get this program.
Uma said:
1 decade ago
What is the meaning of if(!(0))? Please tell.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers