-2

I was toying with the concept of array pointers. I wrote this simple program:

#include <stdio.h>

int main (int argc, char **argv){
    char s[] = "Hello world!\n";
    char *i;
    for (i = s; *i; ++i){
        printf(i);
    }
    return 0;
}

which gives the very amusing output:

Hello world!
ello world!
llo world!
lo world!
o world!
 world!
world!
orld!
rld!
ld!
d!
!

When I wrote this, however, I was under the impression that the output would start from the second row. Reason being, in the for loop, I use a pre-increment notation. i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.

That was my impression, but obviously it is erroneous as the block executes before i gets incremented. I rewrote the program using a post-increment notation and got exactly the same result which confirms my hypothesis. If that is the case, then how are they treated differently in this program?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Morteza R
  • 2,229
  • 4
  • 20
  • 31
  • 1
    The increment ++i occurs after the body of the loop has been executed. So in the first loop i == s is true. – jensa Mar 13 '15 at 10:44
  • 1
    In a `for` loop, it dosen't matter if you type `++i` or `i++` as the third argument. It is the same as using `i++;` and `++i;`. Both of these are equivalent as long as they aren't used in expressions as answers below shows. – Spikatrix Mar 13 '15 at 10:50
  • @pmg That's not why he was confused; regardless of whether he'd used ++i or i++, the increment isn't executed before the first test of *i in the for loop. –  Mar 13 '15 at 10:52

6 Answers6

3

The increment expression of the for loop is executed after the body. For your case

for (i = s; *i; ++i){
  printf(i);
}

is similar to

i = s;       // loop init
while (*i)   // loop condition
{
  printf(i);
  ++i;       // loop increment
}
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
3
for (initialization; condition; increase) statement;

This for works in the following way:

  1. initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.

  2. condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.

  3. statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.

  4. increase is executed, and the loop gets back to step 2.

  5. the loop ends: execution continues by the next statement after it.

There is no difference between postincrement and preincrement because increase is executed separately anyway.

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31
2

i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.

Not quite. The actual syntax is

  1. i is set to the beginning of s

  2. the Boolean condition is checked

    2.1. if it holds true the block executes,

    2.2. come out of loop otherwise.

  3. then i gets incremented and continue to step 2.

Note: In this particular scenario, pre and post increment to i are not going to make any difference.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

the equivalent of

for( a ; b ; c )
{
  statements
}

is

a ;
while( b ) 
{
  statement
  c ;
}

and

++i ;
i++ ;

are the same things, because i is only evaluated and the evaluation isn't used

Guiroux
  • 531
  • 4
  • 11
1

Reason being, in the for loop, I use a pre-increment notation. i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.

No. i is incremented after the block executes.

I rewrote the program using a post-increment notation and got exactly the same result which confirms my hypothesis. If that is the case, then how are they treated differently in this program?

They're not. Post-increment or pre-increment isn't going to make a blind bit of difference. The difference between those two is in the result of the expression i++, ++i (namely, will it evaluate to the previous value or to the new value?). It does not magically alter the entire flow of an encapsulating control structure. The evaluated result of the increment expression is just thrown away so it doesn't matter what you do as long as you provide an expression that results in incrementing i.

It's like how this:

int main()
{
    int x = 5;
    int y = 5;

    int a = x++;
    int b = ++y;
}

will result in different values for a and b because the result of the increment expression is used, whereas the following programs:

int main()
{
   int x = 5;
   x++;
}

int main()
{
   int x = 5;
   ++x;
}

are identical.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

i gets incremented after the block get executed.

Try this:

for(int i = 0; i < 5; ++i )
{
  printf("\n %d",i);
}
CreativeMind
  • 897
  • 6
  • 19