in Programming in C reopened by
579 views
2 votes
2 votes
int a = 50, b = 50;

a = a+++++b;

b = b+++++a;

Value of b after executing these .

I think it should be undefined behaviour. But in answer it is given 155. Can anybody verify it please.
in Programming in C reopened by
579 views

1 comment

It should be undefined behaviour in C.
2
2

1 Answer

–1 vote
–1 vote

consider the 1st statement, b is pre increment and a is post increment

a= 50 + 51

=101 but final value of a will be 102 as post increment

simillarly 2nd statement a= 51 + 103

= 154 but final value of b will be 155 as post increment

3 Comments

Please correct the statement it should be b = 51 + 103 =154 and after that final value will be 155 due to b bas been post incremented. But nice explanation. Thanks!
0
0

but final value of a will be 102 as post increment

Can you explain why it is incrementing to 102?

From my understanding I’m getting the answer 153 as follows:
a = (a++) + (++b);

resulting in a =101 like you. But how come its post incrementing again to 102? Am I missing something?

Then finally:
b = (b++) + (++a);

b= 51 + 102 (a is pre-incremented now)

b = 153

Please correct me if I’m wrong.

0
0

Students always have plenty of time to waste but not to do useful things :)

https://gateoverflow.in/62411/Undefined-behaviour-in-c

0
0

Related questions

0 votes
0 votes
0 answers
1