Understanding Pre and Post Incrementation in C

  • Thread starter Thread starter the_kool_guy
  • Start date Start date
AI Thread Summary
The discussion centers on the confusion surrounding pre and post incrementation in C, particularly with expressions involving multiple increments. It highlights that the results of these expressions are undefined according to the C/C++ standard, as they modify the same variable multiple times without a sequence point. The participants clarify that pre-increment (++i) takes precedence over addition, but the overall behavior remains unpredictable across different compilers and optimization settings. The conversation emphasizes the importance of understanding these concepts for exams, suggesting that students should recognize when results are undefined. Ultimately, mastering these operators and their implications is essential for effective programming in C.
the_kool_guy
Messages
37
Reaction score
0

Homework Statement


initially i = 10;


Homework Equations


1) x = i++ + i++
2) x = ++i + ++i
3) x = i++ + ++i
4) x = ++i + i++
5) x= i++ + ++i + i
6) x= ++i + i++ + i


The Attempt at a Solution


i++ is post incrementation while ++i is pre incrementation
by this logic i know ans of eqn 1) is 20.but other equations gets me wrong
by gcc, answers to 2),3),4),5),6) are 24,22,22,33,33 .
i am getting no idea how this thing works.
somehow i am able to think of a way to answer above but situation gets worse in case of
x = i++ + ++i + i++ + ++i;
and x= ++i + i++ + ++i + i++;
where answers are 45 and 46.
this whole thing is screwing me up
pls help
thanks
 
Physics news on Phys.org
Which has precedence, preincrement or +? Preinc does.

i = 10
2)++i + ++i == 12 + 12 because the preincrement is done BEFORE the addition, AND it is does TWICE.

Sometimes it helps to break down compound expressions into separate sequential lines in order of their precedence.
++i + ++i becomes
++i;
++i;
i + i;
The ++i's come first b/c they are higher in precedence.
 
I've got some bad news for you.

According to the C/C++ standard, the value of all of these expressions is undefined.
That is, each compiler implementation might give a different result.
Furthermore, depending on compiler flags (typically optimization), the results might come out different as well.

At least in the current implementation of gcc, the results are the same independent of the optimization flags, but in previous versions this was not so.

Cheers! :smile:
 
I like Serena said:
According to the C/C++ standard, the value of all of these expressions is undefined.
Bingo.

The results of each and every one of these expressions is undefined. Each expression is modifying an object, the variable i in this case, more than once between sequence points. That is quintessential undefined behavior.
 
TylerH said:
Which has precedence, preincrement or +? Preinc does.

i = 10
2)++i + ++i == 12 + 12 because the preincrement is done BEFORE the addition, AND it is does TWICE.

Sometimes it helps to break down compound expressions into separate sequential lines in order of their precedence.
++i + ++i becomes
++i;
++i;
i + i;
The ++i's come first b/c they are higher in precedence.

this way answer comes in multiple of number of terms.
what about the value of x in which answer is 45 and 46...
 
D H said:
Bingo.

The results of each and every one of these expressions is undefined. Each expression is modifying an object, the variable i in this case, more than once between sequence points. That is quintessential undefined behavior.

that means there is no logic to define these behavior?
but this is surely got to come in my exams... :(
 
I like Serena said:
I've got some bad news for you.

According to the C/C++ standard, the value of all of these expressions is undefined.
That is, each compiler implementation might give a different result.
Furthermore, depending on compiler flags (typically optimization), the results might come out different as well.

At least in the current implementation of gcc, the results are the same independent of the optimization flags, but in previous versions this was not so.

Cheers! :smile:
The results of each and every one of these expressions is undefined. Each expression is modifying an object, the variable i in this case, more than once between sequence points. That is quintessential undefined behavior.[/QUOTE]

that means there is no logic to define these behavior?
but this is surely got to come in my exams... :(
 
the_kool_guy said:
that means there is no logic to define these behavior?
but this is surely got to come in my exams... :(

If it does, choose the answer that says: the result is undefined.
Or perhaps the one that says: the result is implementation defined.

And if you want to practice your skills at these operators, try this one:

What does the following do?

Code:
while (*d++ = *s++);

where s and d are char pointers.

Or this one?

Code:
for (unsigned i = 5; i-- > 0;)
{
    printf("%d", i);
}
 
Who looks like an idiot? I do! :redface:

Yet another good reason to use -Wall.
 

Similar threads

Back
Top