#include<stdio.h>
int main()
{
while(printf("Hello"))
return 0;
}
Produces only Hello
as a output
#include<stdio.h>
int main()
{
while(printf("Hello"));
return 0;
}
Second code prints Hello
for infinite times.
#include<stdio.h>
int main()
{
while(printf("Hello"))
{}
return 0;
}
Third code also prints Hello
for infinite times.
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.
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.
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.
"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).
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"))
{}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With