0

In the following piece of code:

#include <stdio.h>
#include <stddef.h>

void main ()
{
    int n[5] = {1,2,3,4,5};
    
    printf("%s%13s\n","element","value");
    for (size_t i = 0; i<5; ++i)
    {
           printf("%7d%13u\n", i, n[i]);
        }
    
}

the output is:

element        value
  0            1
  1            2
  2            3
  3            4
  4            5

What I don't understand is how i which is pre-incremented, gives off the values from 0 to 4.

I believe it should be 1 to 4, since it would not pass the condition.

What's the reasoning behind this?

sarah
  • 135
  • 5
  • 1
    the third part of the `for` loop statement (the iteration expression) is always executed *after* an iteration finishes – UnholySheep Aug 31 '20 at 07:16
  • And it should be `int main(void)` to be correct, usually with a `return 0;`. – Jens Aug 31 '20 at 07:19
  • 1
    @UnholySheep I used your comment in last part of my answer, when I realised while typing, that I 'd get very close to saying the same. Hope you do not mind. – Yunnosch Aug 31 '20 at 07:20

3 Answers3

1

The counter i is only pre-incremented inside ++i; in the third part of the loop definition. That is a complete statement in which pre or post increment is irrelevant.
The observable behaviour of i is unrelated to the increment, because all output is done in the separate body of the loop.

As Unholy sheep mentions in a comment:

the third part of the for loop statement (the iteration expression) is always executed after an iteration finishes

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

To further illuminate the comment's and the other answer's point that

the third part of the for loop statement (the iteration expression) is always executed after an iteration finishes

the manual unrolling of that loop would be

#include <stdio.h>
#include <stddef.h>

void main ()
{
    int n[5] = {1,2,3,4,5};
    
    printf("%s%13s\n","element","value");
    size_t i = 0;
    printf("%7d%13u\n", i, n[i]);
    ++i;
    printf("%7d%13u\n", i, n[i]);
    ++i;
    printf("%7d%13u\n", i, n[i]);
    ++i;
    printf("%7d%13u\n", i, n[i]);
    ++i;
    printf("%7d%13u\n", i, n[i]);
    ++i;
}
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Please fix my misquote (broken markdown) of Unholy Sheep comment. Otherwise nice visualisation, making together a nice answer with mine. – Yunnosch Aug 31 '20 at 07:26
0

What I don't understand is how i which is pre-incremented

It isn't, i and n[i] are totally independent in that printf loop.

  • i still increases from 0 to 4
  • n[i] then has corresponding values from 1 to 5. The index i is still in range [0,4].

By the way, you should use %zu for size_t type.

for (size_t i = 0; i < 5; ++i)
{
    printf("%7zu%13d\n", i, n[i]);
}
artm
  • 17,291
  • 6
  • 38
  • 54
  • about `%zu`, this code snippet is from the book **C how to program**, and they used `%u`, can you calrify on why to use `%zu` instead? – sarah Aug 31 '20 at 07:28
  • 1
    `%u` is for unsigned type, `%zu` is for `size_t` spefically – artm Aug 31 '20 at 07:28