相关疑难解决方法(0)

参数名称省略,C++ vs C.

在C++中,我倾向于在某些情况下省略参数的名称.但是在C中,当我省略参数的名字时出错了.

这是代码:

void foo(int);  //forward-decl, it's OK to omit the parameter's name, in both C++ and C

int main()
{
    foo(0);
    return 0;
}

void foo(int)  //definition in C, it cannot compile with gcc
{
    printf("in foo\n");
}

void foo(int)  //definition in C++, it can compile with g++
{
    cout << "in foo" << endl;
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?我不能省略C函数定义中的参数名称吗?

c c++ compilation

39
推荐指数
3
解决办法
2万
查看次数

标签 统计

c ×1

c++ ×1

compilation ×1