#include <stdio.h>
int x = 77; /*全局变量x*/
void fn1()
{
extern int y; /*使用外部全局变量y,如果不加extern编译不通过*/
printf("fn1(): x=%d,y=%d\n",x,y); //x=77,y=88;
}
void fn2()
{
extern int y; /*使用外部全局变量y,如果不加extern编译不通过*/
y=888; /*修改外部全局变量y为888*/
printf("fn2(): x=%d,y=%d\n",x,y); //x=77,y=888;
}
int y = 88; /*全局变量y*/
void fn3()
{
printf("fn3(): x=%d,y=%d\n",x,y); //x=77,y=888,y无须使用extern因为y定义在fn3()前面。
}
int main()
{
int x = 10; /*main方法内的局部变量x*/
if(x>0)
{
int x = 100; /*if语句内的局部变量x*/
x/=2;
printf("if语句内, x=%d\n",x); //x=50;
}
printf("main方法内, x=%d\n",x);//x=10;
fn1();
fn2();
fn3();
getch();
return 0;
}
运行结果:
------------------------------------------
if语句内, x=50
main方法内, x=10
fn1(): x=77,y=88
fn2(): x=77,y=888
fn3(): x=77,y=888
时间: 2024-10-03 14:02:55