Understanding Pre and Post Incrementation in C

  • Thread starter Thread starter the_kool_guy
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the behavior of pre and post incrementation in C, particularly focusing on the evaluation of various expressions involving these operators. Participants explore the implications of undefined behavior as defined by the C/C++ standard, and how different compilers may yield different results for the same expressions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant states that the expressions involving pre and post incrementation yield different results, with specific examples provided.
  • Another participant emphasizes that preincrement has precedence over addition, suggesting a breakdown of expressions into sequential lines to clarify evaluation order.
  • Several participants assert that the results of the expressions are undefined according to the C/C++ standard, noting that modifying the same variable multiple times between sequence points leads to undefined behavior.
  • There is a mention that different compiler implementations may produce varying results, and that optimization flags can also affect outcomes.
  • A participant expresses concern about the undefined behavior potentially appearing on exams, questioning the logic behind such behavior.
  • Another participant suggests that if such questions arise in exams, the best answer would be to state that the result is undefined or implementation defined.
  • One participant humorously reflects on their confusion regarding the topic, indicating a personal struggle with understanding the material.

Areas of Agreement / Disagreement

Participants generally agree that the behavior of the expressions is undefined according to the C/C++ standard. However, there are differing views on how to approach understanding and evaluating these expressions, with some advocating for a breakdown of operations while others focus on the implications of undefined behavior.

Contextual Notes

The discussion highlights the limitations of understanding pre and post incrementation due to the undefined behavior associated with modifying the same variable multiple times. This creates uncertainty in predicting outcomes across different compilers and optimization settings.

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

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 42 ·
2
Replies
42
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K