C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 10)
10.
What will be the output of the program?
#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ? &b : &a) = a ? b : c;
printf("%d, %d, %d\n", a, b, c);
return 0;
}
Answer: Option
Explanation:
Step 1: int a=0, b=1, c=3; here variable a, b, and c are declared as integer type and initialized to 0, 1, 3 respectively.
Step 2: *((a) ? &b : &a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3). Hence it return the value '3'.
The left side of the expression *((a) ? &b : &a) becomes *((0) ? &b : &a). Hence this contains the address of the variable a *(&a).
Step 3: *((a) ? &b : &a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a has the value '3'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "3, 1, 3".
Discussion:
19 comments Page 1 of 2.
Bhoomika Maheshwari said:
7 years ago
Considering all above statements as true:
Complete explanation is;
Initially, a=0, b=1, c=3
Then next statement *((a) ? &b : &a) = a ? b : c; means
(i) due to "=" operator RHS is evaluated first which means
if (a)=true then printf {b}; else printf {c};
As a = 0 (which means condition is false as 0 represent false and 1 represent true)
(ii) evaluating LHS as (a)=true then printf {&b}; else printf {&c};
Similarly a= 0 i.e. false.
(iii) now the condition becomes
*(&a)=c;
as * and & operator are compliment to each other therefore, they cancel each other and it become;
a= 3 (value of c is assigned instead writing c).
Now printf("%d, %d, %d\n", a, b, c);
Become 3, 1, 3 as the output,
Thanks.
Complete explanation is;
Initially, a=0, b=1, c=3
Then next statement *((a) ? &b : &a) = a ? b : c; means
(i) due to "=" operator RHS is evaluated first which means
if (a)=true then printf {b}; else printf {c};
As a = 0 (which means condition is false as 0 represent false and 1 represent true)
(ii) evaluating LHS as (a)=true then printf {&b}; else printf {&c};
Similarly a= 0 i.e. false.
(iii) now the condition becomes
*(&a)=c;
as * and & operator are compliment to each other therefore, they cancel each other and it become;
a= 3 (value of c is assigned instead writing c).
Now printf("%d, %d, %d\n", a, b, c);
Become 3, 1, 3 as the output,
Thanks.
(7)
Ajay joshi said:
4 years ago
@All.
It's still not clear can someone tell me why the answer changes if i switch the value of 'a' to some other number than 0?
And also explain the use of ternary operator.
It's still not clear can someone tell me why the answer changes if i switch the value of 'a' to some other number than 0?
And also explain the use of ternary operator.
(2)
Mohana Priya said:
1 decade ago
Thanks for nice explanation.
(1)
Arun said:
5 years ago
Clear explanation, Thanks @Bhoomika Maheshwari.
(1)
Sanjoy said:
1 decade ago
Step 2:
*((a) ? &b : &a) = a ? b : c;
R.H.S - a ? b:c; is a conditional expression where true would mean the first value (b) and false would give the other value(c)
Now, value of a=0 which means the expression before the ternary operator (?) would be false so the answer would be c(i.e 3).
L.H.S - *((a) ? &b : &a) First of all this is a pointer.
Secondly it contains a conditional expression again.
Expression - (a) ? &b : &a
where a=0(false) so the answer would be &a
Now, the expression turns to *(&a)
A pointer which says the variable at the address &a(which would be a itself) has the value as same as R.H.S.
So,*(&a)=3
or a=3
*((a) ? &b : &a) = a ? b : c;
R.H.S - a ? b:c; is a conditional expression where true would mean the first value (b) and false would give the other value(c)
Now, value of a=0 which means the expression before the ternary operator (?) would be false so the answer would be c(i.e 3).
L.H.S - *((a) ? &b : &a) First of all this is a pointer.
Secondly it contains a conditional expression again.
Expression - (a) ? &b : &a
where a=0(false) so the answer would be &a
Now, the expression turns to *(&a)
A pointer which says the variable at the address &a(which would be a itself) has the value as same as R.H.S.
So,*(&a)=3
or a=3
(1)
Avinash said:
1 decade ago
Can anyone explain this programm clearly ?
SMV said:
1 decade ago
I guess in c compiler, doesn't allow to write any expression on left side of assignment, it will throw a error saying that.
L value is required in function main.
L value is required in function main.
Pavan said:
1 decade ago
How can you say conditional operator is true are false?
Ricardo said:
1 decade ago
@Pavan.
In C true is represented by any numeric value not equal to 0 and false is represented by 0.
In the example above a = 0, therefore, the condition is false.
In C true is represented by any numeric value not equal to 0 and false is represented by 0.
In the example above a = 0, therefore, the condition is false.
Deepa said:
1 decade ago
I can't understand step2.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers