gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:02:56 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com