+ 1

pre decrement

what is the output can anyone explain! int a=10, b; b= --a + --a; printf ("%d", a); printf ("%d", b);

10th Sep 2021, 11:58 AM
Saran M
Saran M - avatar
7 Réponses
+ 3
It will print 8 & 17
10th Sep 2021, 12:01 PM
Saurabh
Saurabh - avatar
+ 3
What you are seeing here is undefined behaviour ( or more appropriately an implementation defined behaviour ) because your program is changing value of "a" multiple times between 2 sequence points. Such constructs are are forbidden in C/C++ as output of such programs can't be predicted and would vary depending on the language implementation you are using.( in most of the cases, your compiler should also warn you about the same )
10th Sep 2021, 12:54 PM
Arsenic
Arsenic - avatar
+ 1
Thank u for ur response ..but i got some different answer as 8 and 16.... Please check it out Yahel Saurabh Kumar Yadav
10th Sep 2021, 12:10 PM
Saran M
Saran M - avatar
+ 1
Saran_37, You are right... if anyone knows why- I'd also like to know :)
10th Sep 2021, 12:14 PM
Yahel
Yahel - avatar
0
8 and 17. Pre-decrement decrements the value by 1 and only then does the wanted operation with it.
10th Sep 2021, 12:03 PM
Yahel
Yahel - avatar
0
When I run in compiler it shows... 8 and 16 ...can anyone explain
10th Sep 2021, 12:08 PM
Saran M
Saran M - avatar
0
Answer is 8 and 16
10th Sep 2021, 6:14 PM
Jagdeep Pandey
Jagdeep Pandey - avatar