C11 标准声明:
5.1.2.2.1 Program startup
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
andargv
, 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 asint
, or the type ofargv
can be written aschar ** 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!")]){/*...*/}
最佳答案
是的,这都包含在“或同等”中。关于重命名参数或使用 typedef
ed 类型的脚注只是示例。
我最喜欢的变体是
int main(int argc, char* argv[argc+1]){/*...*/}
因为它拥有关于所有 main
函数语义的最多信息。
关于c - 这是 main() 的有效定义吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000852/