What will be the output of the given code?
What will be the output of the given ce
void main
{
int k=(20>10)?printf("20") printf("10");
printf("%d"100)
getch();
Answers
The output of the given cőde is 20100
Let's correct the question
What will be the output of the given cőde?
#include <stdio.h>
void main()
{
int k = (20 > 10) ? printf("20") : printf("10");
printf("%d", 100);
}
Explanation
? is a Conditional Operator
On the left side of conditional operator (?), a condition is given, and on the right side of conditional operator (?), two values or statements are given which are separated by colon (:)
Now, if the condition is true which is present on the left side of the conditional operator (?) then the output will be value or statement present on the left side of the colon (:) and if the condition is false then the output will be value or statement present on the right side of the colon (:)
Now, if we see the question then the condition is (20 > 10) which is true so, the value or statement on the left side of the colon (:) will be the output i.e. 20 due to print statement printf("20")
After that, there is another print statement which is to print 100 that is why our final output will be 20100
If the condition would have been (20 < 10) i.e. false then the output would have been 10100