Don*_*ild 1 c gcc compiler-warnings
最近我经历了一些类似的代码:(代码是专有的,因此添加了类似的代码)
#include<stdio.h>
void test_it(var)
{
printf("%d\n",var);
}
int main()
{
test_it(67);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test_it没有提到数据类型的参数.
我编译为gcc test_it.c......:令人惊讶的没有警告/错误
我再次编译使用:gcc -Wall test_it.c...:再次没有警告/错误
(现在变得更具侵略性......)
我使用以下方法再次编译它:gcc -Wall -Wextra test_it.c...:
warning: type of ‘var’ defaults to ‘int’最后我得到了警告.
我尝试使用多个参数:
void test_it(var1, var2)
{
printf("%d\n%d\n",var1, var2);
}
int main()
{
test_it(67,76);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
同样的行为!!
我也试过这个:
void test_it(var)
{
printf("%d\n",var);
}
main() // Notice that no `int` there
{
test_it(67);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码-Wall仅提供选项警告.
所以我的问题是为什么int数据类型对于函数定义中的函数参数不是必需的?
编辑:
重写问题:
为什么在省略函数参数的数据类型的情况下gcc不给出警告-Wall,但是在省略函数返回类型时给出警告?为什么在第一种情况下忽略它?