7

Possible Duplicate:
Parameter evaluation order before a function calling in C

For the below code I expected the output to be 20 and 76 but instead 75 and 21 is comming as output .Please explain why is so.

    #include<stdio.h>

    unsigned func(unsigned n)
    {

       unsigned int a =1 ;        
       static unsigned int b=2;        
       a+=b; b+=a;        
       {

         unsigned int a=3;        
         a+=b; b+=a;        
       }
       //printf("%d %d ",a,b);
       return (n+a+b);        
    }
    int main()
    {
        printf("%d %d\n",func(4),func(5));
        return 0;
    }
Community
  • 1
  • 1
user1543957
  • 1,758
  • 4
  • 20
  • 30
  • I haven't confirmed it, but I strongly suspect this: http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c – Mysticial Oct 25 '12 at 07:18

8 Answers8

10

you are expecting func(4) to be called before func(5) but the opposite happens with your compiler. The order of evaluation of function parameters is unspecified by C standard. So, compiler is free choose which function to call first. So across different runs you may observe different order of function calls, though it's very unlikely to happen that way with the same compiler.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Wow. So this renders all printf-style funcs and possibly a bunch of others to be not portable. – SomeWittyUsername Oct 25 '12 at 07:26
  • 4
    @icepack why do you think so? If standard doesn't mandate a specific order and you are going to write code that relies on *unspecified* behaviour, it means your *code* is not portable, not *printf()*. – P.P Oct 25 '12 at 07:33
  • This is a well described scenario called "function side effect" which can occur in almost every language and should be avoided. http://stackoverflow.com/a/13063692/986760 – fkl Oct 25 '12 at 07:35
  • Moreover, this can be created without any printf but using global or static variables and calling functions modifying them, and then again using the function call as assignment to same variables – fkl Oct 25 '12 at 07:37
  • @KingsIndian yep, you're right, I wasn't precise. But I'm sure there is enormous amounts of code everywhere that is written this way without being aware of such behavior. – SomeWittyUsername Oct 25 '12 at 07:47
3

The order of evaluation of func(4) and func(5) isn't defined by the C standard(s).

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
2

The order of evaluation of expression is unspecified behaviour hence func(4) and func(5) may be called in different order as you supposed to

You might like to visit it for more

Compilers and argument order of evaluation in C++

Parameter evaluation order before a function calling in C

Community
  • 1
  • 1
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • You mean unspecified. The program exhibits unspecified behavior, but it does not exhibit undefined behavior as far as I can tell. (“The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified” C99 6.5.2.2:10) – Pascal Cuoq Oct 25 '12 at 07:22
  • @Pascal : Yeah you are right ..I meant that actually – Omkant Oct 25 '12 at 07:27
1

The arguments are pushed onto stack in reverse order. It seems in your compiler implementation, func(5) is called before func(4).

Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
1

Order of evaluation could be the reason. Because,

//printf("%d %d\n",func(4),func(5));
        printf("%d \n",func(4));
        printf("%d \n",func(5));

prints

20 
76
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

func(5) is getting executed first :

Values of variables after executing func(5) :

a = 3
b = 13
func(5) = 21

Since, b is static, the values after executing func(4):

a = 14
b = 57
func(4) = 75
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
0

The code is simple, and remember static variables will preserve their values between function calls.

In your program, due to your compiler(thus compiler specific, not defined by standards):

func(5) is executed first, : which returns 21.. explanation:

    unsigned func(unsigned n) /*first with 5*/
    {

       unsigned int a =1 ;        
       static unsigned int b=2;        
       a+=b; b+=a;        // a = 3, b = 5
       {

         unsigned int a=3;        
         a+=b; b+=a;        // a = 8, b = 13
       }
       //printf("%d %d ",a,b);
       return (n+a+b);        // 5 + 3 + 13 = 21. 
    }

func(4) is executed next,

explanation:

    unsigned func(unsigned n) /*first with 5*/
    {

       unsigned int a =1 ;        
       static unsigned int b=2;        
       a+=b; b+=a;        // a = 14, b = 27
       {

         unsigned int a=3;        
         a+=b; b+=a;        // a = 30, b = 57
       }
       //printf("%d %d ",a,b);
       return (n+a+b);        // 4 + 57 + 14 = 75(which is printed first). 
    }

Hence the output.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

There is a well known term for this called "function side effects". You change a variable inside a function, call the function and rely upon a statement using the variable expecting it to have already changed. Generally this should be avoided and is not a good approach.

A better alternate in such a scenario is either call function and store the return values and then use them in printf or make two different printf calls.

side effects

fkl
  • 5,412
  • 4
  • 28
  • 68