c - 这是 main() 的有效定义吗

标签 c standards language-lawyer c11

C11 标准声明:

5.1.2.2.1 Program startup

  1. The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ }
    

    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    int main(int argc, char *argv[]) { /* ... */ }
    

    or equivalent; 10), or in some other implementation-defined manner.


10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

我们将忽略这部分:或以其他实现定义的方式。因为我只对等同于上述两个示例的定义感兴趣。

这是否是 main 的有效定义,因为 char* a[4]char** 是等价的:

int main(int argc, char* argv[4]){/*...*/}

VLA 数组怎么样,我们假设 printf 将返回一个正整数值:

int main(int argc, char* argv[printf("Hello there!")]){/*...*/}

最佳答案

是的,这都包含在“或同等”中。关于重命名参数或使用 typedefed 类型的脚注只是示例。

我最喜欢的变体是

int main(int argc, char* argv[argc+1]){/*...*/}

因为它拥有关于所有 main 函数语义的最多信息。

关于c - 这是 main() 的有效定义吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000852/

相关文章:

在 C 中组合 Struct + malloc

user-interface - 常用按钮的标准放置顺序

c++ - 具有可变参数模板和 va_args 的 function_traits

linq - 匿名方法/Lambda 的(编码标准)

c++ - 如何检查 T 是否为聚合类型?

c++ - 尽管有 SFINAE,这个程序的格式是否错误?

c - 使用 PBC 将一个元素散列到另一个元素中

c - 调试 C 代码

ios - 使用 Xcode 中现有的 C 代码为 iOS 创建静态库

windows - 通过批处理或类似方式在Windows嵌入式标准上设置互联网时间服务器