关于main(int x)... main(void) [英] regarding main(int x)...main(void)

查看:173
本文介绍了关于main(int x)... main(void)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序的输出是什么

what is the output of this programme

#include <stdio.h>

int main(int x)
{
    printf("Hello, World!\n");
main(10);
    return 0;
}





并假设我们提到main(10)

而不提及其原型为main(int x)那么为什么结果仍然被多次打印?

并假设我使用主(无效)术语然后实际意味着什么......我们是主要的10次或者所以?



and suppose if we mention main(10)
without mentioning its prototype as main(int x) then why is the result still being printed multiple times?
and suppose if i use the main(void) term then what it actually means...are we calling main 10 times or so?

推荐答案

该程序运行两次,因为在main函数内部你再次调用函数并猜测它是什么,它永远不会结束......它将不断调用main函数并将值为10的无用参数传递给它,并再次在屏幕上打印消息。



使用 main(void)表示你根本没有传递任何参数,而 main(int a)表示需要整数数据类型的参数,但它不会缩短hello world打印的次数。如果你愿意,你可以把它写成,



That program runs twice, because inside the main function you're again making a call to the function and guess what, it will never end... It will continously call the main function and pass that (useless) parameter of value 10 to it, and again print the message on the screen.

Using main(void) means that you're not passing any parameter at all, whereas main(int a) means a parameter of integer data type is required, but it doesn't cut short the number of times the hello world will print. If you want to, you can write it as,

#include <stdio.h>

int main(int x)
{
    // create a loop to print the values
    for (int i = 0; i < x; i++)
       printf("Hello, World!\n");
    return 0;
}





调用main函数并立即传递值。您可以使用相同的逻辑,通过使用for循环调用main函数10次等等。



Call the main function and pass the value now. You can use the same logic, to call the main function 10 times by using a for loop and so on.


还可以查看此页面: C-recursion [ ^ ]。
Have also a look at this page: C-recursion[^].


这篇关于关于main(int x)... main(void)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆