int a=5; int b; b= ++a + ++a; printf("%d", b); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

int a=5; int b; b= ++a + ++a; printf("%d", b);

The output to this one is different for Java and C... in C it says 14.. in Java it says 13.... Which is correct? And how does it work?

28th Mar 2017, 2:35 PM
Manoj Kumar S
7 Answers
+ 11
its all about increment operator. as in java ++ means +1 and its before a so +1 before a in the initial value n at every step value changes and at last stored in b so as a =5 b= 1+a + (1+a)+1//as the changes are made in default value b=(1+5) + (1+(5+1)) b=6 + 7 b=13//your ans **this is the program pattern in blue j environment hope it helps you
29th Mar 2017, 5:54 AM
Saumya
Saumya - avatar
+ 8
yes 😊😊
29th Mar 2017, 11:02 AM
Saumya
Saumya - avatar
+ 3
well I can tell you about C not about Java. In C all the pre-increments are carried out first: in this statement we have 2 pre-increaments so your a=5 becomes a=7. now adding them 7 + 7 is giving you 14. sorry, but no idea about Java internal expr. evaluation.
28th Mar 2017, 3:04 PM
GeekyShacklebolt
GeekyShacklebolt - avatar
+ 3
This might be worth a read: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points In Java ++a is a function with return value a+1, which increases a by 1 as a side effect (while a++ returns a, but has the same side effect). Hence it would be consistent with the output to assume, that the code is just executed from left to right. Take this with a grain of salt though, I'm not an expert on Java and its internal workings.
28th Mar 2017, 3:21 PM
Tob
Tob - avatar
0
But the logic of execution has to be same irrespective of the programming language right??
29th Mar 2017, 6:10 AM
Manoj Kumar S
0
Guess answer for the following and then write code to cross check int a = 1; Int b = a++ + ++a + a++ + ++a ; System.out.print(b);
9th Nov 2023, 5:44 AM
G Sahithi
G Sahithi - avatar
0
G Sahithi My guess is 12. Is that right? Unable to copy paste this code to compiler. Too lazy to manually copy paste it 😂
9th Nov 2023, 2:19 PM
Manoj Kumar S