在 C 中只将一个参数传递给 main()
Pass only one argument to main() in C
我 运行 这个程序在 Linux 上的 GCC(gcc 版本 4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04.3))。它使用 gcc myprog.c
和 运行 成功编译,没有任何警告或错误。
那么,为什么在 C 中只为 main
提供一个参数时编译器不给出任何警告或错误?
#include <stdio.h>
int main(int i)
{
printf("%d\n", i);
}
按照 C 标准
5.1.2.2.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; or in some other implementation-defined manner.
如果实现将其定义为接受单个参数,那么它可以。
使用 GCC 6(在 Linux/Debian/Sid/x86-64 上)我的编译器在使用 gcc -Wall rsp.c -o rsp
您的 rsp.c
示例进行编译时会给出一些警告。
rsp.c:3:5: warning: ‘main’ takes only zero or two arguments [-Wmain]
int main(int i)
^~~~
但是 C11 编程语言(阅读 n1570) does not specify when a compiler should warn. It mostly defines what are the correct possible programs and how should they behave. Read more about undefined behavior(我想,但我不确定,你有一个)。
如果您想真正了解您的计算机上实际发生的情况,请研究编译器的源代码,发出的汇编代码(使用 gcc -S -fverbose-asm -O rsp.c
编译然后查看生成的 rsp.s
),与您的系统相关的 C standard library, of your crt0 runtime. Study also the ABI and the calling conventions 的源代码。
我想你应该非常害怕未定义的行为。非常bad things could happen.
作为在 Linux 上编码时的实用建议,始终使用 -Wall
进行编译,并将 -g
传递给 gcc
-Wall
选项要求几乎所有警告(您可以添加 -Wextra
)。 -g
选项要求提供调试信息。然后,您将能够使用 gdb
调试器来了解更多运行时发生的情况。
一旦您对自己的代码有信心并对其进行了调试,请考虑使用 -O
或 -O2
编译它以请求 optimization (in particular for benchmarking). (BTW, gcc
can accept both -g
and -O2
). Read documentation on GCC command options.
我 运行 这个程序在 Linux 上的 GCC(gcc 版本 4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04.3))。它使用 gcc myprog.c
和 运行 成功编译,没有任何警告或错误。
那么,为什么在 C 中只为 main
提供一个参数时编译器不给出任何警告或错误?
#include <stdio.h>
int main(int i)
{
printf("%d\n", i);
}
按照 C 标准
5.1.2.2.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
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; or in some other implementation-defined manner.
如果实现将其定义为接受单个参数,那么它可以。
使用 GCC 6(在 Linux/Debian/Sid/x86-64 上)我的编译器在使用 gcc -Wall rsp.c -o rsp
您的 rsp.c
示例进行编译时会给出一些警告。
rsp.c:3:5: warning: ‘main’ takes only zero or two arguments [-Wmain]
int main(int i)
^~~~
但是 C11 编程语言(阅读 n1570) does not specify when a compiler should warn. It mostly defines what are the correct possible programs and how should they behave. Read more about undefined behavior(我想,但我不确定,你有一个)。
如果您想真正了解您的计算机上实际发生的情况,请研究编译器的源代码,发出的汇编代码(使用 gcc -S -fverbose-asm -O rsp.c
编译然后查看生成的 rsp.s
),与您的系统相关的 C standard library, of your crt0 runtime. Study also the ABI and the calling conventions 的源代码。
我想你应该非常害怕未定义的行为。非常bad things could happen.
作为在 Linux 上编码时的实用建议,始终使用 -Wall
进行编译,并将 -g
传递给 gcc
-Wall
选项要求几乎所有警告(您可以添加 -Wextra
)。 -g
选项要求提供调试信息。然后,您将能够使用 gdb
调试器来了解更多运行时发生的情况。
一旦您对自己的代码有信心并对其进行了调试,请考虑使用 -O
或 -O2
编译它以请求 optimization (in particular for benchmarking). (BTW, gcc
can accept both -g
and -O2
). Read documentation on GCC command options.