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条回答
小情绪 Triste *
2楼-- · 2020-04-05 08:35

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

查看更多
Viruses.
3楼-- · 2020-04-05 08:38

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
查看更多
登录 后发表回答