0

I know similar question has been asked, but I read it before posting my question, and it did not seem to help me.

The following program should return the sum (i+1)*2 + (i+2)*3 that runs from 0 to a certain number:

#include <stdio.h>
#define MULa 2
#define MULb 3
#define NUM 3

int entry( int a, int b )
{        
    return (a* MULa + b* MULb);
}

int sum(int num)
{
    int my_sum = 0;
    int i = 0, j = 0;

    while (j <= num)
    {
        printf("in iteration %d before incrementing: i is  %d\n", j, i);
        my_sum += entry(i++, i++);
        printf("sum is %d in iteration %d\n", my_sum, j);

        j++;

        printf("after incrementing: i is %d\n", i);
    }
    return (my_sum);
}

int main()
{         
    int my_sum = sum( NUM );
    printf("sum is %d\n", my_sum);
    return 0;
}   

Now, the program does not do what it is supposed to.

From what I understand, through each iteration i is incremented twice, and this is done after the function call. I printed out the values of sum and expected the following result:

    in iteration 0 before incrementing: i is 0
    sum is 8 in iteration 0     /*(0+1)*2 + (0+2)*3 = 8*/
    after incrementing: value of i is 2
    in iteration 1 before incrementing: i is 2
    sum is 26 in iteration 1     / *8 + (2+1)*2 + (2+2)*3 = 26*
    after incrementing: i is 4
    in iteration 2 before incrementing: i 4
    sum is 54 in iteration 2     /*26+(4+1)*2 + (4+2)*3 = 54*/
    after incrementing: i is 6
    in iteration 3 before incrementing: i 6
    sum is 92 in iteration 3     /*54 + (6+1)*2 + (6+2)*3 = 92*/
    after incrementing: i is 8
    sum is 68

But instead, I got this:

in iteration 0 before incrementing: i 0
sum is 2 in iteration 0
after incremeting: i is 2
in iteration 1 before incrementing: i 2
sum is 14 in iteration 1
after incremeting: i is 4
in iteration 2 before incrementing: i 4
sum is 36 in iteration 2
after incremeting: i is 6
in iteration 3 before incrementing: i 6
sum is 68 in iteration 3
after incremeting: i is 8
sum is 68

I don't really understand why that's the output. How come sum is 2 for example, in iteration 0? I'd be glad if you could explain how this function works, what actually occurs in each step.

Thanks in advance!

Tree
  • 145
  • 1
  • 13
  • 3
    I recommend you read [this evaluation order refernece](http://en.cppreference.com/w/c/language/eval_order). It will tell you that there is no ordering or sequencing between evaluation of function arguments, leading to `entry(i++,i++)` being *undefined behavior*. If you want to pass `i + 1` then do that, and *later* increase `i`. – Some programmer dude Feb 17 '17 at 15:16
  • @Some programmer dude Thanks! – Tree Feb 17 '17 at 15:26
  • How come in printf it is OK to pass i++ and in other functions it is not? – Tree Feb 17 '17 at 15:36
  • It's not. If you do e.g. `printf("%d %d\n", i++, i++)` it's just as undefined as what you are doing. – Some programmer dude Feb 17 '17 at 15:40
  • @Some programmer dude Thank you. – Tree Feb 17 '17 at 15:43
  • 1
    @Tree-- you _can_ pass `i++` once: `printf("%d\n", i++)`, but if you pass two arguments like this: `print("%d %d\n", i++, i++)` it is undefined behavior because you are modifying a variable twice between sequence points, and there are sequence points around a function call. – ad absurdum Feb 17 '17 at 15:56
  • @David Bowling. Thanks a lot. So the problem is that i could be incremented twice after exiting from the function entry, or it could be incremented once after evaluating a in entry, and another time after evaluating b in entry? Is the semi colon a sequence point? Can you please explain this in a little more detail? – Tree Feb 17 '17 at 16:08
  • @David Bowling.Actually, problem is that i could be incremented twice when reaching the first coma, or it could be incremented once after reaching the first comma, and another time after reaching the second comma? – Tree Feb 17 '17 at 16:14
  • Sequence points are a bit subtle. Roughly, after a sequence point, every side effect and value computation that occurred before the sequence point has been completed. Sequence points occur in very specific circumstances. You can read about them [in the Standard here](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.3), and [here](http://port70.net/~nsz/c/c11/n1570.html#C), and in many other places scattered throughout the Standard. A semicolon at the end of a full expression marks a sequence point, but there are many other cases. – ad absurdum Feb 17 '17 at 16:20
  • Yes, the problem is that there are several possibilities for the value of `i` since the order of the operations is unspecified. – ad absurdum Feb 17 '17 at 16:23

0 Answers0