0

int a =5; int x = ++a + ++a + ++a;

Need explanation why it comes 22 in output when we print in C language

4th Aug 2017, 9:43 AM
SAURAV KUMAR
SAURAV KUMAR - avatar
26 ответов
+ 2
well, the sum needs two operand, so ++a + ++a increments a twice a = 5 ++a + ++a = 14 then ++a = 8 so ++a + ++a + ++a = 14+8
4th Aug 2017, 5:10 PM
Andrés04_ve
Andrés04_ve - avatar
+ 9
As I have said, multiple prefix/postfix operators applied to a variable between to sequence points will result in undefined behaviour. The result is completely compiler dependent. Also, stop using Turbo C++. https://www.sololearn.com/Discuss/288609/?ref=app
4th Aug 2017, 4:49 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
@sayan chandra It means that when you use multiple prefix, or postfix operators on a single variable between two sequence points (usually two semicolons), it is up to the compiler to determine which operator is performed first. This results in different values for different compilers, and hence undefined.
4th Aug 2017, 5:46 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
The sole fact that Andres got 22 and sayan got 24 has already proven my point. Please refer, and do some research: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points Your results are compiler dependent.
4th Aug 2017, 5:42 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
woah sololearn didn't let me post the answer
4th Aug 2017, 5:08 PM
Andrés04_ve
Andrés04_ve - avatar
4th Aug 2017, 5:10 PM
Andrés04_ve
Andrés04_ve - avatar
+ 1
yep, I was trying to answer before but i couldn't post anything :p
4th Aug 2017, 5:14 PM
Andrés04_ve
Andrés04_ve - avatar
0
IT'S NOT UNDEFINED BEHAVIOR
4th Aug 2017, 5:07 PM
Andrés04_ve
Andrés04_ve - avatar
0
exactly what i am saying....andré##✌✌
4th Aug 2017, 5:12 PM
sayan chandra
sayan chandra - avatar
0
@sayan chandra as far as i know for arithmetic operations the compiler operatea two variable with a operand at a time not 3 at once...
4th Aug 2017, 5:33 PM
SAURAV KUMAR
SAURAV KUMAR - avatar
0
ya ...i know....but see i am generalising the idea if u have a=5 and doing ++a adding n times the reselt is ..(a+n)*n
4th Aug 2017, 5:34 PM
sayan chandra
sayan chandra - avatar
0
I'm using code blocks 16.01 with gcc gnu compiler
4th Aug 2017, 5:41 PM
SAURAV KUMAR
SAURAV KUMAR - avatar
- 1
HERE IS THE LEGIT EXPLAINATION... there is 3 (++a) so first c compiler wont do the sum... it will first determine what is the last incremented value of a... 3 increment.. value is (5+1+1+1)...i.e. 8 now it will calculate ...like 8+8+8= 24 trust me...i ran the code... and similarly if u do... ++a + ++a + ++a + ++a(4 times) ans is 9+9+9+9= 36 last incremented value is 9
4th Aug 2017, 4:26 PM
sayan chandra
sayan chandra - avatar
- 1
yes the logic is perfectly right... because when you do pre decrement.. just like this... --a + --a + --a the last decremented value is.. 5-1-1-1 = 2 so ans is 2+2+2 = 6 right on my laptop screen..
4th Aug 2017, 4:29 PM
sayan chandra
sayan chandra - avatar
- 1
and the most importent thing is... in c language code...its not written whether to do all increment (3 times) at once then sum up or another process...(particularly for one program) like one increment then sum then again increment then sum...(particularly for this program) so if you have 4 compilers in c they may have different approach####like what to do in such program...how to compute and what to compute next.. and again updates wont change logic...
4th Aug 2017, 4:57 PM
sayan chandra
sayan chandra - avatar
- 1
the language c is fixed... compilers can be mordern... its totally okay to avoid turbo c++ but that does not make the logic wrong...
4th Aug 2017, 5:00 PM
sayan chandra
sayan chandra - avatar
- 1
which compiler u r using... it seems u getting 22...
4th Aug 2017, 5:39 PM
sayan chandra
sayan chandra - avatar
- 1
ooh andré got 22 also.... but please tell me the compiler. cause in here its 24
4th Aug 2017, 5:41 PM
sayan chandra
sayan chandra - avatar
- 1
rei...wil you please shorten the meaning of that link.. i mean just tell what is the breif idea.. the page is large... is it...compiler varies...calculation varies ???
4th Aug 2017, 5:44 PM
sayan chandra
sayan chandra - avatar