in Programming in C
521 views
0 votes
0 votes
Output will be-

int main()

{ int a =0,b=1,c=3:

*((a)?&b:&a)=a ? b : c;

printf("%d %d%d ", a,b,c);

}

O/P AT LAST ?
in Programming in C
521 views

2 Comments

From where you get this beautiful question ?

 

Basic point, &* are inverse to each other.

*((a)?&b:&a) = a ? b : c;

if you clearly observe, 

*((a)?&b:&a) is simply as,

if a=0, then it is *( address of a ) ===> assigning value to a

if a=1, then it is *( address of b ) ===> assigning value to b

But why it is written, like * ( & ) ? due to they apply ternary operator ==> it result r-value but not l-value, So they indirectly use like this.

 

First evaluate right side of "=" operator ( as per associativity and precedence rule ) ==> which results value of c, i.e., 3

Assign it to a

∴ a = 3

 

Now i have a doubt, that " Is it lead to undefined behavior ? "

Ans : NO, think why it is not undefined behavior.

1
1

 question is from let us c : )

i am not getting u ...

see how i approach as ternary is right to left operator so first a=0 which is false so we evaluate c which is 3 ..

now goes inton this

*((a)?&b:&a)

a is false again inside so i am evaluating *(&a ) =a ?b:c

now at this step i dont know what to do after this

0
0

1 Answer

1 vote
1 vote
Best answer

Precedence of () > ?: > =

So, ${}^*((a)?\&b:\&a)= (a ? b : c);$

${}^*((a)?\&b:\&a) = {}^*(\&a)$, because a = 0

$a ? b : c = c$, as a = 0

${}^*(\&a) = c, \,a=3$

printf("%d %d%d ", a,b,c); 

prints $3,1,3$

selected by

4 Comments

as far as I know there is no order of evaluation, there is just precedence, the order of evaluation completely depends on the compiler, so the above code can evaluate right or left depends on the compiler.
You can correct me if I'm wrong
0
0
how can order of evaluation can be avoided with precedence and associativity, they are different terms.
0
0
sorry it's my mistake !
0
0