Wrong output while running C code [duplicate]

2020-04-05 08:04发布

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;
    }

标签: c
8条回答
一纸荒年 Trace。
2楼-- · 2020-04-05 08:12

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

查看更多
可以哭但决不认输i
3楼-- · 2020-04-05 08:17

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

查看更多
Evening l夕情丶
4楼-- · 2020-04-05 08:20

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
查看更多
家丑人穷心不美
5楼-- · 2020-04-05 08:33

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

查看更多
Explosion°爆炸
6楼-- · 2020-04-05 08:33

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.

查看更多
来,给爷笑一个
7楼-- · 2020-04-05 08:34

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.

查看更多
登录 后发表回答