#include <stdio.h>
int main(int k)
{
if(k<10)
printf("%d ",main(k+1));
return k;
}
输出为:
10 9 8 7 6 5 4 3 2
在main()
函数的参数中,它的argc
但是这里如何使用它?
最佳答案
首先,您的 main
签名是标准定义的。你的编译器应该给出警告:
[Warning] 'main' takes only zero or two arguments [-Wmain]
C11: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 ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used1,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.
现在,您可以为 argc
和 argv
指定任何名称。这里argc
是k
。由于您没有向 main
传递任何参数,因此 k
的值为 1
,因为这里 argv[0]
是程序的名称。现在k=1
被程序用作初始值和值
10 9 8 7 6 5 4 3 2
是通过递归调用main
打印的。
<子>1。重点是我的。
关于c - 以下程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818070/