Mayur Fartade
Mayur Fartade

Reputation: 317

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

Second code

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

Second code prints Hello for infinite times.

Third code

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

Third code also prints Hello for infinite times.

Compiler used - GCC 9.0.1

why this is happening?

Upvotes: 6

Views: 3051

Answers (5)

Ajanyan Pradeep
Ajanyan Pradeep

Reputation: 1132

First Code:

printf("Hello") returns the number of characters.

When printf("Hello") is used inside while loop it will print Hello and return 5.

Since it is greater than 0 while loop consider this as true and execute the statement below the while, which is return 0.

The return 0 makes the main function to return 0 and stop the exeution.

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 ;. So the statement return 0 is not executed and while checks the condition infinite times printing infinite Hello.

Third code:

While will execute the statements only within the { }.

Since its empty every time after searching for statement it will go back and check the condition.

Since the condition is always true it will not reach the return 0 and it will print Hello infinite times.

Upvotes: 1

Petr Skocik
Petr Skocik

Reputation: 60068

while takes a statement after the closing ).

6.8.6 Iteration statements

 iteration-statement:
                while ( expression ) statement

 ....

In

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

that statement (which is basically while's argument) is return 0; (6.8.6)

In

while(printf("Hello"));

the statement is ; (an empty (null)/expression statement (6.8.3)).

In

while(printf("Hello")){}

it's an empty compound statement ({}, 6.8.2), which is semantically equivalent to ;.

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"))
    {}

Upvotes: 10

Eraklon
Eraklon

Reputation: 4288

In the first one the body of the while is the return 0 so it will return after the first iteration. 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;
    }

Upvotes: 2

Adrian Mole
Adrian Mole

Reputation: 51835

In your first code snippet, the return 0; statement is part of the while loop's 'body'; 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.

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

Upvotes: 2

NoShady420
NoShady420

Reputation: 941

printf returns number of characters printed (which is 5). Any non zero number evaluates to true. So the loop is an infinite loop.

The rest depends on what happens withing the loop. 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 breaks the control flow out of the loop causing the loop (and in this case the program) to stop executing

Upvotes: 2

Related Questions