0
int a =5; int x = ++a + ++a + ++a;
Need explanation why it comes 22 in output when we print in C language
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
+ 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
+ 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.
+ 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.
+ 1
woah sololearn didn't let me post the answer
+ 1
yep, I was trying to answer before but i couldn't post anything :p
0
IT'S NOT UNDEFINED BEHAVIOR
0
exactly what i am saying....andré##✌✌
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...
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
0
I'm using code blocks 16.01 with gcc gnu compiler
- 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
- 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..
- 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...
- 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...
- 1
which compiler u r using...
it seems u getting 22...
- 1
ooh andré got 22 also....
but please tell me the compiler.
cause in here its 24
- 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
???