这是src1.c的内容:
#include
extern int w;
//int go(char); // no need to declare here. WHY????
main(){
char a='f';
go(a);
printf("%d\n", w);
}
这是src2.c的内容:
#include
int w = 99;
int go(char t){
printf("%c\n%d\n",t,sizeof(t));
}
为什么在Linux中编译后,在src1.c文件中声明go函数是不是必须?
cc src1.c src2.c;
编译器是否将src2.c文件中的go函数定义放在main函数的代码上面,这样就不需要声明了?
我这样做:
#include
int go(char); // need to declare here, because if not, arguments of go will be promoted to intS and they would conflict with char parameters defined in go. Error is droped!
main(){
char a='f';
go(a);
}
int go(char t){
printf("%c\n%d\n",t,sizeof(t));
}
所以每个人都说,在没有原型的情况下传递任何数量和类型的参数是错误的.在这种情况下,它们被提升为整数,但必须同意定义中指定的那些.
我做了一些测试,发现即使它编译没有错误它也无法正常工作.
SRC1:
#include
int go(int t){
printf("%d\n%d\n",t,sizeof(t));
}
sr2.c:
#include
int go(int); //if I omit this prototype, program outputs 1 which is far from correct answer :)
main(){
double b=33453.834;
go(b);
}
所以最后答案只能是未定义的行为.
谢谢Maxim Skurydin