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 2 of 2.
Nikhil said:
10 years ago
Suppose a = 1. What answer should be?
Yashwanth said:
10 years ago
I don't understood none of explanation.
RatanNarayana Gangoni said:
9 years ago
Can anyone explain this clearly?
Teju said:
8 years ago
@Sanjoy.
How is this *(&a)=3?
a = 0 right
Explain it.
How is this *(&a)=3?
a = 0 right
Explain it.
Sandeep said:
8 years ago
How to solve nested conditional operator?
Right to Left (or) Left to Right?
Right to Left (or) Left to Right?
Rupinderjit said:
1 decade ago
* and & are compliment to each other.so they cancelled each other and a is left which assign value 3.
Since,=has right to left associativity so right side expression will get execute first gives 3.
Tehn comes to left side and we get a.
So a==3 ultimately.So do the answer.
Since,=has right to left associativity so right side expression will get execute first gives 3.
Tehn comes to left side and we get a.
So a==3 ultimately.So do the answer.
Nani said:
5 years ago
Thanks for explaining @Bhoomika Maheshwari.
Srinu said:
1 decade ago
Can any one explain.
Nani said:
1 decade ago
In step2 *& gives value so that c value is assigned to a variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers