Increment & decrement operators in C/C++ (warning answer varies from compiler to compiler)

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Some things that need to be pointed out:

First, _precedence_ does not determine _order of evaluation_ . The relative precedence of ++ and + only determines how the expression is _parsed_, not the order in which subexpressions are evaluated. b = ++a + ++a + ++a; is parsed as b = (((++a) + (++a)) + (++a)); However, each ++a may be evaluated _in any order_ ; none of the arithmetic operators (+, -, *, /, or %) force left-to-right evaluation.

Secondly, the ++ operator has a _result_ and a _side effect_ . The _result_ of ++a is the current value of a + 1; the _side effect_ is that a is incremented. However, the side effect does not have to be applied immediately after evaluation; it may be deferred until the next sequence point (alternately, the side effects may all be applied before any other evaluation takes place). For a well-defined expression like b = ++i + j++;, the evaluation order may go something like this:

t0 <- i + 1
t1 <- j
i <- i + 1
j <- j + 1
b <- t0 + t1

The expression ++a + ++a + ++a is undefined because it is attempting to modify the value of an object through the evaluation of an expression more than once without an intervening sequence point. Similarly, attempting to update an object and use its value in a computation without an intervening sequence point also invokes undefined behavior.. The results of doing so are not guaranteed to be repeatable, and will depend on the compiler, compiler settings, even the surrounding code. All of the following forms invoke undefined behavior:

x = x++
a++ * a++
a[i]=i++
foo(a++, a++)

etc.

For anybody wondering why they're getting a different result, it's _because_ the behavior is undefined, and literally *any* result is possible, whether it makes sense or not. If you're really curious about how you're getting a specific result, look at the machine code generated by the compiler. Just don't expect that behavior to be consistent from program to program.

For reference, the operators &&, ||, ?:, and the comma operator all force left-to-right evaluation, and all introduce a sequence point between the evaluation of the left-hand operand and the right-hand operand. So an expression like a++ && a++ && a++ is well-defined.

*EDIT*

Also, for people asking about results they get in Java, remember that C and Java are different languages with different rules. In Java, all expressions are evaluated from left to right and side effects are applied immediately, so ++a + ++a + ++a gives you the result you expect = 11 + 12 + 13 == 36.

johnbode
Автор

int a=10;
int b=++a + ++a + ++a;
cout<<b;

I got 37 !!??

How the compiler calculated the result 37 ???

BareedKhaled
Автор

Sir please tell me one question
What will be the value of m if n=10;
m+= n-- + n++ -n;

allicsesolutions
Автор

hi sir as far as my knowledge all the three operators are preincrement so according to associvity rule it should go from right to left not from left to right

praveenpadala
Автор

hi, thanks for explaining
but what about this
a=5;
b=++a + a++ * --a;

oguzgurler
Автор

int res = a++ + ++a + a++ + ++a; pls sir explain this one.

harishkataria
Автор

Dear Sir, I use xcode for c programming and i am getting result 36. why?? and for a++ + a++ + a++, i am getting 33. plz answer me. why? xcode is not an older IDE? Then why is it showing like that?

myangel_mylife
Автор

Explanation of answer 37
here it is using grouping method
b= (12+12)+13
a increase by 2 and reach at 12 then it will perform addition. After that it will reach at 13 and finally 24+13=37

manojfmm
Автор

but when am compiling it over c it's giving 37 in java 36 can you please explain, I know it's compiler dependent, but tell me only for c. please reply

yatinarora
Автор

Sir I have One Question:
When i am solve this Question then different C Compiler is Different Output
Plz Explain it.
#include <stdio.h>
#include <conio.h>
void main()
{
int a=1, b;
clrscr();
b=a++ + ++a + ++a;
printf("%d %d", b, a);
getch();
}

Output in Turbo C++ 3.0 is : "b=9 and a=4".
Output in Dev C++ 5.4.0 is : "b=7 and a=4".
Output in Code Block 16.01 is : "b=8 and a=4".

abznews
Автор

a=10;
if b= ++a + ++a + ++a;
then according to pre-increment we should increase by 1 and substitute that
the b= 13 + 12 + 11
since associtivity is from right to left

editz
Автор

u have not explained for b = ++a + ++a = 24. how ?

chakraprasad
Автор

This is undefined behavior, don’t do this

brianflynn
Автор

int a=4, b;
b=++a+ ++a+ ++a+ ++a+ ++a;
I am getting a=9 and b=36 in c language....I am not understanding how b comes out to be 36.plz explain

upinderkaur
Автор

a=(++x) + (x++); my answer for a is coming is 12 but the real answer is 13. please explain this.

solidname
Автор

there is no a specific answer for this... it is compiler dependent😏😏😏😏

letscih
Автор

perfect stuff learned a lot bro thanks

swarnapudisomesh
Автор

Bullshit !
It totally depends on which compiler you are using to run the code. the same expression might give an output of 37 . Its really frustrating that, this type of question still gets priority in some interviews.

RezaulKarim-cqft
Автор

hi sir mera answer turbo c mei 39 but Mobile online compiler mei 37 aa rha hai.ek hi question ka do alag alag answer kaise aa sakta hai sir?

snehajitchandra
Автор

Can you tell me please what should be the answer
int a=10;
b=a++ + a++ + a++;
b=?
and exact what logic to be followed to compute prefix-post fix numericals?

suryachakraborty