0

Today I revisited Pre Increment and Post Increment.

Basic definitions I know.

Pre Increment - Increments the Value and returns the value.

Post Increment - Increments the Value and returns the value prior to increment.

But Doing some combinations of them i am stumped.

Using a basic C program, here is what I tested.

With i =0 initially.

1st Test

printf("%d %d",++i,++i);

Ouput:

2 2

I Expected:

1 2

2nd Test

printf("%d %d",i++,i++);

Ouput:

1 0

I Expected:

0 1

3rd Test

printf("%d %d",i++,++i);

Ouput:

1 2

I Expected:

0 2

4th Test

printf("%d %d",++i,i++);

Ouput:

2 0

I Expected:

1 1

I figured the evaluation might be from the right side or the left side. Maybe from left in case of Pre Increment & Right in case of Post increment. Maybe Pre Increment had a higher priority than Post increment. Some ideas to match the results, but assumption made on one test doesn't explain the other output.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Shaurya Chaudhuri
  • 3,772
  • 6
  • 28
  • 58
  • 3
    All undefined behaviour. – chris Oct 15 '14 at 01:42
  • 1
    For C related to [Why are these constructs undefined behavior?](http://stackoverflow.com/q/949433/1708801) and [Is the output of printf (“%d %d”, c++, c); also undefined?](http://stackoverflow.com/q/10845122/1708801) – Shafik Yaghmour Oct 15 '14 at 01:43
  • Change your tests to something like `int before = i++; printf("%d %d\n", before, i);` to get rid of the undefined behavior. – 5gon12eder Oct 15 '14 at 01:44
  • 1
    For each of those constructs, whatever you *intended* it to do, there's a better way to do it. – Keith Thompson Oct 15 '14 at 01:48
  • "I figured the evaluation might be from the right side or the left side" -- **Why** did you figure that? As has been explained at this site and elsewhere many many many many times, the order *isn't specified*. You will get different results on different systems and even different versions of the same compiler. – Jim Balter Oct 15 '14 at 02:08
  • I was trying to figure out an explanation. Its not that i thought it was the case for a long time. – Shaurya Chaudhuri Oct 15 '14 at 02:10

1 Answers1

6

Everything you have is undefined behavior because you are modifying the same variable multiple times between the same pair of sequence points. For example

i = i++;

is undefined as well. There's a deeper discussion here, and there's a nice slideshare that covers this and more "deep C" quirks.

The other issue is the order of evaluation. The arguments are evaluated in an unspecified order, so if you have

f(a(), b(), c());

it can call a, b, and c in any order.

You're mixing undefined behavior and unspecified behavior, so although you could venture a guess to explain why you get the output you do, it's hard to give a satisfactory explanation since it's so random.

Community
  • 1
  • 1
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • I see , i didn't know it was an undefined behavior. But in university i have faced many a times problems such as - int b = i++ + ++i etc etc. So these questions aren't valid questions at all? – Shaurya Chaudhuri Oct 15 '14 at 02:05
  • @ShauryaChaudhuri Indeed, they are not valid questions at all (in C; in Java the order of evaluation is strictly defined) ... unless they are test questions that follow up on course work explaining that they are undefined. – Jim Balter Oct 15 '14 at 02:14
  • @ShauryaChaudhuri I've seen questions on test like that where it says "what are the possible values of b?" or similar, the answer is that it could be anything. the program could crash, your computer could catch fire, anything. However, a lot of professors don't worry about low level details like that, so be prepared to pull out the C standard if you want to show that you are right. – Ryan Haining Oct 15 '14 at 02:17
  • Well they were as test questions, but undefined behavior was never taught. And they were mostly given as objective questions with specific values like (1,2) , (2,1). And sometimes different professors gave different explanations. I searched online a bit more and found that java or c# enforces a specific left to right order so that these arent ambiguous? – Shaurya Chaudhuri Oct 15 '14 at 02:20
  • @ShauryaChaudhuri Java, C# and C are different languages and have significantly different rules in many areas. C and C++ have plenty of undefined behavior whereas Java strives hard to prevent UB. Take this [question](http://stackoverflow.com/q/18402304/1708801) as an example and I am sure you can find many more where Java and C# have well defined behavior while C and C++ does not. – Shafik Yaghmour Oct 15 '14 at 02:27
  • @ShauryaChaudhuri so I've heard, but I don't know the java or c# specs, I can only speak to c and c++ – Ryan Haining Oct 15 '14 at 02:27
  • @ShauryaChaudhuri please read the two questions I mentioned in my comments to your question they both have answers that provide standard citations that explain why this is undefined behavior in C and the behavior of such program can not be relied on. – Shafik Yaghmour Oct 15 '14 at 02:29
  • @ShafikYaghmour & Ryan - Read the answer. But what will happen in this case? Suppose i have i = foo(&i)+foo(&i)+foo(&i); And the foo function increments the value using i++. Is it still an undefined behavior. Because though i main it might not have different sequence points but when there is a function call there will be sequence points i guess? – Shaurya Chaudhuri Oct 15 '14 at 02:45
  • @ShauryaChaudhuri see [Sequence point from function call?](http://stackoverflow.com/q/18516510/1708801), there is probably a better question on this but I don't have the brain power to find it now. – Shafik Yaghmour Oct 15 '14 at 03:05