我是C的新手.我遇到过一种我以前从未见过的函数语法形式,参数类型在参数列表之后定义.有人可以向我解释它与典型的C函数语法有何不同?
例:
int main (argc, argv)
int argc;
char *argv[];
{
return(0);
}
Run Code Online (Sandbox Code Playgroud) 如果"a function"是单独编译的,那么就不会检测到不匹配,"函数"会返回一个main,它将main视为一个int ...根据我们所说的声明如何必须匹配定义,这可能会似乎令人惊讶.可能发生不匹配的原因是,如果没有函数原型,则函数通过其在表达式中的第一次出现来隐式声明,例如
sum += "the function"(line);
Run Code Online (Sandbox Code Playgroud)
如果先前未声明的名称出现在表达式中并且后跟左括号,则上下文将其声明为函数名称,假定该函数返回int,并且不假设其参数.
我事先为这个含糊不清的问题道歉,但这是什么意思?
顺便说一下,这是Brian W. Kernighan和Dennis M. Ritchie的C编程语言书第2版第73页第4.3章.
最近我经历了一些类似的代码:(代码是专有的,因此添加了类似的代码)
#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仅提供选项警告.
所以我的问题是为什么 …