简体   繁体   English

无法理解C程序的输出

[英]Not able to understand the output of C program

There's this code in C C中有这段代码

int fun()
{
  static int num = 40;
  return num--;
}

int main()
{
  for(fun(); fun(); fun())
  {
    printf("%d ", fun());
  }
  getchar();
  return 0;
}

the output comes out to be: 38 35 32 29 26 23 20 17 14 11 8 5 2 输出结果为:38 35 32 29 26 23 20 17 14 11 8 5 2

I'm not able to figure out why the program doesn't continue to print beyond 2. ie in the negatives .shouldn't it continue printing ... -1 -4 -7....infinite loop Can anyone explain ? 我无法弄清楚为什么该程序不能继续打印到2以上。即,在底片中。它不应该继续打印... -1 -4 -7 ....无限循环有人可以解释吗?

fun() is evaluating to 0 here: fun()的计算结果为0

for(fun(); fun(); fun())
//         ^    

0 is the equivalent of false in C, so the loop is exiting. 0等于C中的false ,因此循环退出。

The code relies on the fact that num - 1 is a multiple of 3, as fun() is evaluated once at the start of the loop and then 3 times every loop. 该代码依赖于num - 1是3的倍数的事实,因为fun()在循环开始时被评估一次,然后在每个循环中被评估3次。 If, for example, you changed the definition to 例如,如果您将定义更改为

static int num = 41;

fun() would return 0 in the wrong place and your loop would continue into the negative numbers. fun()将在错误的位置返回0 ,并且循环将继续为负数。

The whole magic happens inside your for() loop. 整个魔术发生在for()循环内。

The basic format is like this: 基本格式如下:

for (<pre-iteration>; <condition>; <post-iteration>)
    <code>

Code inside pre-iteration will be executed once before the first iteration/loop. 预迭代中的代码将在第一次迭代/循环之前执行一次。 Code inside condition is evaluated before each and every iteration. 每次迭代之前都会评估代码内部条件 If the resulting value is true , the loop continues, if it's false , the loop is left. 如果结果值为true ,则循环继续;如果结果为false ,则循环结束。 Code inside post-iteration is executed after each and every iteration. 迭代后的代码在每次迭代之后执行。

To make the whole code easier to understand, it's important to know that you're able to write such a for() loop using a while() loop as well: 为了使整个代码更易于理解,重要的是要知道您也可以使用while()循环编写这样的for()循环:

<pre-iteration>
while (<condition>) {
    <code>
    <post-iteration>
}

Back to your example: 回到您的示例:

for (fun(); fun(); fun())
{
    printf("%d ", fun());
}

Using a while() this can be written like this: 使用while()可以这样写:

fun();
while (fun()) {
    printf("%d ", fun());
    fun();
}

Since fun() will return the value of the static value num and then decrement it by 1 the first iteration will look like this: 由于fun()将返回静态值num的值,然后将其递减1因此第一次迭代将如下所示:

// This is going to be the first call, so the static variable is initialized: num = 40
fun();                    // returns 40; num = 39
while (fun()) {           // returns 39; num = 38 (true -> loop continues)
    printf("%d ", fun()); // returns 38; num = 37 (-> 38 is written to console)
    fun();                // returns 37; num = 36
}

The next iteration (code before the loop is no longer executed for obvious reasons): 下一次迭代(出于明显的原因,不再执行循环前的代码):

fun();                    // no longer executed
while (fun()) {           // returns 36; num = 35 (true -> loop continues)
    printf("%d ", fun()); // returns 35; num = 34 (-> 35 is written to console)
    fun();                // returns 34; num = 33
}

This continues till the last iteration: 这一直持续到最后一次迭代:

fun();                    // no longer executed
while (fun()) {           // returns 0; num = -1 (false -> loop exits)
    printf("%d ", fun()); // no longer executed
    fun();                // no longer executed
}

I'm not able to figure out why the program doesn't continue to print beyond 2. ie in the negatives .shouldn't it continue printing ... -1 -4 -7....infinite loop Can anyone explain ? 我无法弄清楚为什么该程序不能继续打印到2以上。即,在底片中。它不应该继续打印... -1 -4 -7 ....无限循环有人可以解释吗?

Yes, it might continue, but only if that specific call to fun() wouldn't return 0 , ie set the initial value of num to 41 and the program will run indefinitely (or at least till the return value happens to be 0 at some point). 是的,它可能会继续,但前提是对fun()特定调用不会返回0 ,即将num的初始值设置为41且程序将无限期运行(或至少直到返回值恰好在0变为0时)一点)。

Let's look at what happens in the second call to fun() in the for loop. 让我们看看在for循环中第二次调用fun()会发生什么。 The first time the loop is entered, fun() has been evaluated once already, so num is 39 . 首次进入循环时,已经对fun()进行了一次评估,因此num39 The second time we get to the loop, fun() has been executed three more times, so it is now 36 . 第二次进入循环, fun()已经执行了三遍,现在是36 This process continues, until eventually, the second call to fun() returns 0 . 此过程一直持续到最终对fun()的第二次调用返回0为止。 In C, 0 means false , so the loop terminates at that stage. 在C中, 0表示false ,因此循环在该阶段终止。

for(fun(); fun(); fun())

second part of for() loop is a condition - if fun() returns 0, which is interpreted as false, the loop terminates. for()循环的第二部分是一个条件-如果fun()返回0(解释为false),则循环终止。 lately i got a brain fart and wrote a statement that condition is true only for positive numbers. 最近,我放屁了,写了一个声明,说条件只对正数才成立。

When 什么时候

printf("%d ", fun());

is run, fun() has value 2 Then the last fun() in the next command is executed to increase the counter (normally): 运行时,fun()的值为2然后执行下一条命令中的最后一个fun()以增加计数器(通常):

for(fun(); fun(); fun())

fun() is 1. fun()为1。

Then the condition to continue the for loop is performed, which is the second fun() in: 然后执行继续for循环的条件,这是第二个fun():

for(fun(); fun(); fun())

At this point, fun() is 0 which makes the for loop stop. 此时,fun()为0,这使for循环停止。

When you print fun() , you do not see the actual value of num , but the value of num+1 . 当您打印fun() ,您看不到num的实际值,而是num+1的值。

This is because you are using a postfix-decrement ( num-- ) and not a prefix-decrement ( --num ). 这是因为您使用的是后缀减量( num-- ),而不是前缀减量( --num )。

So when you 2 is printed, the actual value of num is 1. 因此,当您打印2时, num的实际值为1。

At the end of this iteration, fun() is invoked, setting num to 0. 在此迭代结束时,将调用fun() ,将num设置为0。

At the beginning of the next iteration, num is evaluated as false and the for loop is terminated. 在下一次迭代的开始, num被评估为false,并且for循环终止。

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

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