C Programming :: Control Instructions - Discussion
Home > C Programming > Control Instructions > MCQs Questions Discussion
3 / 33
What is the output of the following 'C' program?
#include<stdio.h>
void main()
{
int i=10,j=20;
j=i,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
#include<stdio.h>
void main()
{
int i=10,j=20;
j=i,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
A10 10
B10 20
C20 10
DCompilation Error
Answer: Option (Login/Signup)
#include
int c=10000;
Show Explanation
- notice the comma operator when variables are separated by them they are evaluated from left to right and only last value is considered ( it has less precedence than assignment operator)
so due to the precedence of assignment being high the assignment is done first and then comma operator is evaluated be the expression finally evaluates to j=i,(j?(i,j)?i:j:j); - so the value of i is assigned to j
you can run the same program with one more variable C the value of j will be equal to the value of C as per above logic
#include
void main()
{
int i=10,j=20;
int c=10000;
j=c,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Asked In ::
STEP-BY-STEP
- notice the comma operator when variables are separated by them they are evaluated from left to right and only last value is considered ( it has less precedence than assignment operator)
so due to the precedence of assignment being high the assignment is done first and then comma operator is evaluated be the expression finally evaluates to j=i,(j?(i,j)?i:j:j); - so the value of i is assigned to j
you can run the same program with one more variable C the value of j will be equal to the value of C as per above logic
#include
void main()
{
int i=10,j=20;
int c=10000;
j=c,j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Read Full Answer
Report Error
Please Login First Click Here