C language function parameter sequence

Look at a choice of a National C Language exam.

Asked the following code output

int func(int a,int b)

{ int c;

c=a+b;

return c; }

main()

{ int x=6,r;

r=func(x,x+=2);

printf ( "% d \ n" , r);}
option is probably 14, 16, wrong not run.

14 election majority of people, there are many believe to be incorrect not to run.

First of all, this code can be executed.

This question is far from difficult, but do not know will be wrong.

This question the answer is 16.

Here Insert Picture Description
For the parameters of the function, when the calculation is performed in accordance with the order from right to left.

For the function func, first find the right parameters x + = 2, returns x = 8, then the calculation results. Is passed to the two parameter values ​​are 8, 16 return value.

Let us write the next program verification, combined with difficulty from under the Operational upgrades.

Consider the following program execution results:

#include<stdio.h>

void fun(int m,int n)

{

printf("%d+%d\n",m,n);

}

int main ()

{

int a=100;

fun(a,a+=2);

printf("%d,%d,%d\n",++a,a–,++a);

printf("%d\n",a);

return 0;

}
Based on the contents mentioned above, you should also think of printf function, parameter calculation should follow from left to right. But when it comes to ++, it will figure out the majority of the students. We look at the results, to help you understand.
Here Insert Picture Description
First, the first line is a validated value of 102 after adding 2 to the value parameter is passed.

Second row, from right to left, the first is to let the value +1 a, output 103. The second is a 103. But the next step is to let a value of -1, and then calculate the parameters of the left, or else?

The operating results, we know that the left argument is first calculated, a value of +1 is output 104, and after a run value -1.

Finally, the output line 103 is also verified the final performance of a value of -1.

Published 240 original articles · won praise 3 · Views 3175

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105163827