0

Why does i++; give me weird random numbers as opposed i + 1; in the for loop? I am quite new to programming so I would like to understand the difference here. My guess is that i is somehow overwritten which makes the compiler take random numbers out of memory.

#include <stdio.h>

int main() {
    int arr[100];

    for (int i = 0; i < 100; i++) {
        arr[i] = i + 1; //why wouldn't i++ work?
    }

    return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • C is case sensitive, so be very, very careful when coding. `main` and `Main` are two different things. – tadman Oct 27 '20 at 18:36
  • 1
    `arr[i] = i++` - which side of the equation gets evaluated first - incrementing the `i` or calculating `arr[i]` reference to assign the value to? – bobah Oct 27 '20 at 18:36
  • 1
    Also see [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) `arr[i] = i++` would be *undefined behavior*. – Some programmer dude Oct 27 '20 at 18:39

4 Answers4

3
arr[i] = i+1;

Fine, that assigns i + 1 to element i in arr.

arr[i] = i++;

Undefined Behavior: There is no sequencing between reading i for arr[i] and modifying it in i++. Anything goes.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

i + 1 computes the value of i plus 1. It offsets i values by 1.

i++ yields the current value of i, but increments i immediately after. This messes up with your loop, effectively skipping places.

The difference here is that the + operator does not alter either value, it produces a new value instead, while the ++ operator changes the value to which it is attached.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

i++ is the postfix increment operator, which means it increments i then returns the old value of i. So, in the first loop you'll get 0 written to arr[0], then i will be incremented to 1, then again by the loop to 2, then 2 will be written to arr[2], and i will become 4, and so on. So, every other value will have the index, and the others will have random uninitialized memory.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • 2
    There's no guarantee about which value of `i` is used on the left hand side of the assignment. `a[i] = i++;` is UB. – rici Oct 27 '20 at 18:38
0

Given this code:

    int arr[100];

    for (int i = 0; i < 100; i ++) {
        arr[i] = i+1; //why wouldn't i++ work?
    }

... there are two issues with substituting i++ for i + 1 in the loop body:

  1. Evaluating the expression i++ updates the value of i, and evaluating the expression a[i] reads the value of i. Because the evaluations of those expressions in your assignment are "unsequenced" relative to each other, the resulting behavior is undefined. That allows anything to happen, not restricted to the effects of choosing one order of evaluation or the other.

  2. Suppose that (1) were not an issue in a given implementation -- one that provides an extension specifying, say, that evaluation of the left operand of an assignment is sequenced before evaluation of the right operand of that assignment. In that case, if you modify the sample code only by substituting the expression i++ for the expression i+1 in the loop body then the result is i being incremented twice for each iteration of the loop. In that event, only half of the array elements would be assigned values by your loop. The values of local variables declared without initializers are indeterminate until explicitly assigned. Reading the values of the unassigned elements could yield any results.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157