小编Ash*_*mar的帖子

Why is the return value of the fun function 8 instead of 7?

With reference to Comma-Separated return arguments in C function [duplicate] ,

x=x+2,x+1;
Run Code Online (Sandbox Code Playgroud)

will be evaluated as

x=x+2; 
Run Code Online (Sandbox Code Playgroud)

However, in case of the following code

#include<stdlib.h>
#include<stdio.h>

int fun(int x)
{
    return (x=x+2,x+1); //[A]
}

int main()
{
   int x=5;
   x=fun(x);
   printf("%d",x); // Output is 8
}
Run Code Online (Sandbox Code Playgroud)

Shouldn't line [A],be evaluated as

x=x+2;
Run Code Online (Sandbox Code Playgroud)

giving x = 7

c return-value comma

35
推荐指数
4
解决办法
2740
查看次数

标签 统计

c ×1

comma ×1

return-value ×1