Understanding Pre and Post Incrementation in C

  • Thread starter Thread starter the_kool_guy
  • Start date Start date
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
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 42 ·
2
Replies
42
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K