Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

while-loop

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?

like image 237
Mayur Fartade Avatar asked Apr 23 '20 12:04

Mayur Fartade


People also ask

Can printf () function be used in if condition?

The expression within if( expression ) is always evaluated, and in your case that's a call to printf . The value of this expression is used to determine if the body (blank in your case) of the if is run. Show activity on this post. printf() function return the number of characters that are printed.

How many times the printf statement within the while loop will be executed?

Answer is NEVER . It means the pointer will never reach inside the LOOP. This is major cause of the PrintF command will never RUN in this program.

Can we use printf () apart from just printing values?

The printf function of C can do a lot more than just printing the values of variables. We can also format our printing with the printf function. We will first see some of the format specifiers and special characters and then start the examples of formatted printing.

What is printf () in C?

"printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (lexing aka. parsing).


Video Answer


2 Answers

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"))
    {}
like image 161
PSkocik Avatar answered Oct 08 '22 19:10

PSkocik


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

like image 29
Abhay Aravinda Avatar answered Oct 08 '22 20:10

Abhay Aravinda