最后结果 等于 5
x+y=4
#include <stdio.h>
int x=1,y=2;
void sub( int y )
{
/*
x=1
y=2
*/
x++;y++;
/*
x=2
y=3
*/
}
void main()
{
int x=2;sub(x);
/*
x=2
y=2
*/
printf("x+y=%d\n",x+y);
}
这道题其实中间那个函数sub有没有结果都是一样的,因为不论它做了什么运算,都只是局部变量,不会带到函数以外来,所以x的值是2,y的值也是2,局部变量和全局变量同名时,在函数内会代替全局变量。