当前位置: 技术问答>linux和unix
外部变量的问题
来源: 互联网 发布时间:2016-10-04
本文导语: 我单独编辑一个test.c文件如下, int a; char b; a = 4; b = 5; 用 gcc -c test.c 编译时就会报错和警告如下: test.c:3: warning: data definition has no type or storage class test.c:4: warning: data definition has no type or storage class...
我单独编辑一个test.c文件如下,
int a;
char b;
a = 4;
b = 5;
用 gcc -c test.c 编译时就会报错和警告如下:
test.c:3: warning: data definition has no type or storage class
test.c:4: warning: data definition has no type or storage class
test.c:4: error: conflicting types for ‘b’
test.c:2: error: previous declaration of ‘b’ was here
现在我改用如下的形式:
int a = 4;
char b = 5;
或者将赋值语句放在一个函数体内时
int a;
char b;
diaoyong()
{
a = 4;
b = 5;
}
就能编译通过生成test.o
这是什么原因
int a;
char b;
a = 4;
b = 5;
用 gcc -c test.c 编译时就会报错和警告如下:
test.c:3: warning: data definition has no type or storage class
test.c:4: warning: data definition has no type or storage class
test.c:4: error: conflicting types for ‘b’
test.c:2: error: previous declaration of ‘b’ was here
现在我改用如下的形式:
int a = 4;
char b = 5;
或者将赋值语句放在一个函数体内时
int a;
char b;
diaoyong()
{
a = 4;
b = 5;
}
就能编译通过生成test.o
这是什么原因
|
这看编译器的实现吧
在函数之外的a=4,编译器只能理解为变量定义,因为函数之外是不能有赋值语句的
在函数之外的a=4,编译器只能理解为变量定义,因为函数之外是不能有赋值语句的