Understanding Pre and Post Incrementation in C

  • Thread starter the_kool_guy
  • Start date
In summary, the value of all of the expressions involving pre-incrementation and post-incrementation is undefined according to the C/C++ standard. Each compiler implementation might give a different result and the answers might vary depending on compiler flags. In exams, it is recommended to choose the answer that states the result is undefined or implementation-defined.
  • #1
the_kool_guy
37
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
  • #2
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.
 
  • #3
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:
 
  • #4
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.
 
  • #5
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...
 
  • #6
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... :(
 
  • #7
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... :(
 
  • #8
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);
}
 
  • #9
Who looks like an idiot? I do! :redface:

Yet another good reason to use -Wall.
 

FAQ: Understanding Pre and Post Incrementation in C

What is the difference between pre-increment and post-increment in C?

In C, pre-increment and post-increment are two unary operators used to increase the value of a variable by one. The main difference between them is the order in which the operation is performed. Pre-increment (++x) first increases the value of the variable and then returns the updated value, while post-increment (x++) first returns the original value and then increases it.

How does pre-increment and post-increment affect the value of a variable?

Pre-increment and post-increment both increase the value of a variable by one, but they do so in different ways. Pre-increment directly modifies the value of the variable, while post-increment creates a temporary copy of the original value, increments it, and then returns the original value. This can affect the behavior of the code in certain situations.

When should I use pre-increment and when should I use post-increment in my code?

The choice between pre-increment and post-increment depends on the specific situation and the desired outcome. Pre-increment is usually preferred when the updated value of the variable is immediately needed, while post-increment is useful when the original value needs to be used in an expression before being incremented.

Can pre-increment and post-increment be used with any data type in C?

Yes, both pre-increment and post-increment can be used with any data type in C, including integers, floating-point numbers, and pointers. They can also be used with user-defined data types, as long as the appropriate operators are overloaded.

Are there any performance differences between pre-increment and post-increment in C?

In most cases, there is no significant performance difference between pre-increment and post-increment in C. However, in certain situations, pre-increment may be slightly faster due to the lack of a temporary copy of the original value. It is always recommended to test and measure the performance in your specific code to determine which one is more efficient.

Similar threads

Replies
12
Views
3K
Replies
10
Views
2K
Replies
3
Views
792
Replies
6
Views
1K
Replies
2
Views
2K
Replies
16
Views
2K
Back
Top