简体   繁体   English

解释交流程序的输出

[英]Explain the output of a c program

#include<stdio.h>
main()
{
    static int i=1;
    printf("\nSoftware");                                                                   
    for(;i<5;i++)
        main();
    return 0;
}

The output comes out to be an infinite loop. 输出结果是一个无限循环。 Please explain. 请解释。

You are calling a main function from your main function. 要调用从函数main函数。 After you call new main function it will print some string and then again it will call main function. 调用新的主函数后,它将打印一些字符串,然后再次调用函数。

Your i variable will not be incremented at all, because it is not incremented in the first iteration of for loop. 您的i变量根本不会增加,因为它在for循环的第一次迭代中不会增加。 After you call main , it will never return to previous main to next iteration of for loop to happen. 调用main之后 ,它将永远不会返回到上一个main到for循环的下一次迭代发生。 So the loop will be infinite and i will be equal to 1 . 因此,循环将是无限的,而将等于1 Even if you changed the for loop to any other loop, the loop will be still infinite. 即使将for循环更改为任何其他循环,该循环仍将是无限的。

I'm including your repaired code, just for the fun of it: 我包括您修复的代码,只是为了好玩:

#include<stdio.h>

int main()
{
    static int i=0;
    if(i>=5) return 0;
    i++;
    printf("\nSoftware %i", i);
    main();
    return 0;
}

When one iteration of Loop finishes then value of i is incremented. 循环的一次迭代完成后, i的值将增加。 But in your case before increment it is again calling the main() function. 但是在您的情况下,在递增之前它会再次调用main()函数。 Hence your Code is in infinite loop. 因此,您的代码处于无限循环中。

It is not an infinite loop. 这不是一个无限循环。

Your are recursively calling main but never give an end to it. 您正在递归调用main但永远不会结束它。 This results in a stack overflow. 这导致堆栈溢出。

i++ is never evaluated, because you never reach the end-of-scope of the for loop. 永远不会评估i++ ,因为您永远不会到达for循环的终点。

This isn't seem to be any difficult or different.In this program for loop do not matter much, so whenever a main() is called inside the same it is definitely a infinite loop.With use of Conditional statements you can restrict the infinite loop. 这似乎没有什么困难或不同。在此for循环程序中没什么大不了的,因此只要在同一个内部调用main()绝对是一个无限循环。使用条件语句,您可以限制无限环。 Also that static int do not matter at all.So obviously main get called infinite number of times until time out occurs. 同样,静态int一点都不重要,因此很明显main被调用了无数次,直到发生超时为止。 This is infact self explanatory 这实际上是自我解释

在循环中调用main函数会耗尽所有内存,并导致堆栈溢出。

Yeah definitely the output comes out to be infinite because in for(;i<5;i++) the increment of i occurs only at the last line of the for loop. 是的,输出肯定是无限的,因为在for(;i<5;i++)中, i的增量仅出现在for循环的最后一行。 So, here the value of i never incremented. 因此,在这里i的价值从未增加。 for example: 例如:

for(i=0;i<5;i++)
{
int a,b; // just an example
a=10;
b=20;
printf("%d",i);
printf("%d %d",a,b);
// increment of i takes place here at the last line of for loop
}

Same as here also: 与这里也一样:

main()
{
    static int i=1;
    printf("\nSoftware");                                                                   
    for(;i<5;i++)
    {    
    main();
    // increment of i takes place here.But compiler never comes at this line .That's why i never incremented .
   }
   return 0;
}

the code call main function, again and again 代码一次又一次地调用主函数

for(;i<5;i++)
main();

Its a kind of Recursive Call. 它是一种递归调用。 Your main function is calling itself resulting into an infinite loop. 您的主要功能是调用自身,导致陷入无限循环。

i<5 call main() i <5呼叫main()
i<5 call main() i <5呼叫main()
i<5 call main() i <5呼叫main()
... ...

If u want to call some function 5 times. 如果你想调用某个函数5次。

include 包括

void myFunc()
{
   printf("\nSoftware"); 
}

main()
{
    static int i=1;                                                                
    for(;i<5;i++)
        myFunc();
    return 0;
}

Im used to c++ so it could be a some small mistake. 我曾经使用过c ++,因此可能是一个小错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM