c - 以下程序的输出

标签 c program-entry-point argc

#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 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 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.

现在,您可以为 argcargv 指定任何名称。这里argck。由于您没有向 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/

相关文章:

c - main(int argc, char *argv[]) 损害了我的程序

C 从命令提示符中获取参数作为条件

c - 使用 fgets 读取 .CSV 文件

c - C中结构内部结构的问题

c++ - 测量 CPU 频率缩放效果

c - 使用 MCU-ATMega 1280 进行多中断实时数据记录

java - 如何在主java中调用Keypress函数

c - : main(){}, int main(){} 和 int main(void){} 有什么区别

Java - 如何结束 while 函数的进程

c - 我对这个 C 程序如何工作的理解正确吗?