简体   繁体   English

为什么将 while() 中的 printf() 作为条件打印不同的 output

[英]Why printf() in while() as a condition prints different output

First code第一个代码

#include<stdio.h>
int main()
{
    while(printf("Hello"))
    return 0;
}

Produces only Hello as a output仅产生Hello作为 output

Second code第二个代码

#include<stdio.h>
int main()
{
    while(printf("Hello"));
    return 0;
}

Second code prints Hello for infinite times.第二个代码无限次打印Hello

Third code第三个代码

#include<stdio.h>
int main()
{
    while(printf("Hello"))
    {}
    return 0;
}

Third code also prints Hello for infinite times.第三个代码也无限次打印Hello

Compiler used - GCC 9.0.1使用的编译器 - GCC 9.0.1

why this is happening?为什么会这样?

while takes a statement after the closing ) . while结束后有一个语句)

6.8.6 Iteration statements 6.8.6 迭代语句

 iteration-statement:
                while ( expression ) statement

 ....

In

while(printf("Hello"))
    return 0;

that statement (which is basically while 's argument) is return 0;该语句(基本上是while的参数)是return 0; ( 6.8.6 ) ( 6.8.6 )

In

while(printf("Hello"));

the statement is ;声明是; (an empty (null)/expression statement ( 6.8.3 )). (一个空的(null)/表达式语句( 6.8.3 ))。

In

while(printf("Hello")){}

it's an empty compound statement ( {} , 6.8.2 ), which is semantically equivalent to ;它是一个空的复合语句( {}6.8.2 ),在语义上等同于; . .

Your code snippets are examples of misleading whitespace—where the whitespace makes humans understand things differently from a compiler.您的代码片段是误导性空白的示例——空白使人类对事物的理解与编译器不同。

Less misleading renderings would be:较少误导的渲染将是:

while(printf("Hello"))
    return 0;

, ,

while(printf("Hello"))
    ; //or perhaps a {} instead of the null statement

and

while(printf("Hello"))
    {}

printf returns number of characters printed (which is 5). printf返回打印的字符数(即 5)。 Any non zero number evaluates to true.任何非零数的计算结果为真。 So the loop is an infinite loop.所以循环是一个无限循环。

The rest depends on what happens withing the loop. rest 取决于循环中发生的情况。 In the second and third cases, the loops are empty (contain no statements) so they keep executing在第二种和第三种情况下,循环是空的(不包含语句),因此它们继续执行

In the first case, return 0 is executed within the loop.在第一种情况下, return 0在循环内执行。 Return breaks the control flow out of the loop causing the loop (and in this case the program) to stop executing Return 将控制流中断循环,导致循环(在这种情况下是程序)停止执行

In your first code snippet, the return 0;在您的第一个代码段中, return 0; statement is part of the while loop's 'body';语句是while循环“主体”的一部分; in fact, it is the entirety of that body, So, on the first run through that loop, the program exits (because that what return 0; does when executed in main ) and the loop is, thus, abruptly terminated.实际上,它是整个主体,因此,在第一次运行该循环时,程序退出(因为return 0;main中执行时会退出)并且循环因此突然终止。

In the second and third snippets, you have an empty body for the loop, but that does not prevent it from running, as the printf("Hello") function call will return the number of characters that were output - which will be non-zero, and thus interpreted as "true".在第二个和第三个片段中,循环体为,但这并不妨碍它运行,因为printf("Hello") function 调用将返回 output 的字符数 - 这将是非零,因此被解释为“真”。

In the first one the body of the while is the return 0 so it will return after the first iteration.在第一个中, while的主体是return 0 ,因此它将在第一次迭代后返回。 Meanwhile with the other two version is the same, having an empty body so they infinitely going on doing nothing but the condition is keep evaluating which will print "hello".同时与其他两个版本是相同的,有一个空的身体,所以他们无限地继续无所事事,但条件是不断评估将打印“你好”。

    while(printf("Hello"))
    return 0;

is same as

    while(printf("Hello"))
    {
        return 0;
    }

First Code:第一个代码:

printf("Hello") returns the number of characters. printf("Hello")返回字符数。

When printf("Hello") is used inside while loop it will print Hello and return 5.当在 while 循环中使用printf("Hello")时,它将打印Hello并返回 5。

Since it is greater than 0 while loop consider this as true and execute the statement below the while, which is return 0 .由于它大于 0 while循环认为这是真的,并执行 while 下面的语句,即return 0

The return 0 makes the main function to return 0 and stop the exeution. return 0使main function 返回 0 并停止执行。

The code编码

while(printf("Hello"))
    return 0;

is same as

while(printf("Hello"))
{
    return 0;
}

Second Code:第二个代码:

Since you used ;既然你用过; after while() ,it will not execute the statement after ; while()之后,不会执行后面的语句; . . So the statement return 0 is not executed and while checks the condition infinite times printing infinite Hello .所以语句return 0没有被执行, while检查条件无限次打印无限Hello

Third code:第三个代码:

While will execute the statements only within the { } . While 将仅在{ }内执行语句。

Since its empty every time after searching for statement it will go back and check the condition.由于每次搜索语句后它都是空的,它会返回 go 并检查条件。

Since the condition is always true it will not reach the return 0 and it will print Hello infinite times.由于条件始终为真,它不会达到return 0 ,它将无限次打印Hello

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

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