相关疑难解决方法(0)

C函数语法,参数列表后声明的参数类型

我是C的新手.我遇到过一种我以前从未见过的函数语法形式,参数类型在参数列表之后定义.有人可以向我解释它与典型的C函数语法有何不同?

例:

int main (argc, argv)
int argc;
char *argv[];
{
return(0);
}
Run Code Online (Sandbox Code Playgroud)

c syntax function

76
推荐指数
5
解决办法
4万
查看次数

C函数调用:理解"隐式int"规则

如果"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章.

c function

4
推荐指数
2
解决办法
9392
查看次数

为什么函数定义中的函数参数不强制提及`int`数据类型?

最近我经历了一些类似的代码:(代码是专有的,因此添加了类似的代码)

#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仅提供选项警告.

所以我的问题是为什么 …

c gcc compiler-warnings

1
推荐指数
1
解决办法
77
查看次数

标签 统计

c ×3

function ×2

compiler-warnings ×1

gcc ×1

syntax ×1