It has been a popular question that how to print hello world without using semicolon.I know many codes but this one sounds weird because I am unable to get the logic behind it.Please help me know how it prints.
if(printf("hello world")){}
It has been a popular question that how to print hello world without using semicolon.I know many codes but this one sounds weird because I am unable to get the logic behind it.Please help me know how it prints.
if(printf("hello world")){}
printf() is a normal function that returns the numbers printed, so basically the code first calls printf() and then checks, whether its return values evaluates to true (i.e. more than 0 characters where outputted). This is the case for "hello world", however it does not matter, since the conditional block is empty anyway.
Take a look at the docs:
int printf ( const char * format, ... );
Return Value
On success, the total number of characters written is returned. On failure, a negative number is returned.
So it returns 12 11 in the Hello World case and that number is interpreted as true value. Value of if test needs to be calculated to decide which code block to execute which means that printf() is called in the first place.
The bit about the semicolons is just a little "I'm smarter than you" misdirection.
However, when you get this you'll know something about c;
Here is a series of programs that may help. Compile and run each one, then think about what they do and how they differ from the ones that came before:
#include <stdio.h>
int main(int argc, char**argv){
int i = printf("Hello, world!\n");
printf("%d\n",i);
return 0;
}
#include <stdio.h>
int main(int argc, char**argv){
if ( 1 ) {
printf("condition evaluated as true\n");
} else {
printf("condition evaluated as false\n");
}
return 0;
}
#include <stdio.h>
int main(int argc, char**argv){
if ( printf("Hello, world!\n") ) {
printf("condition evaluated as true\n");
} else {
printf("condition evaluated as false\n");
}
return 0;
}
#include <stdio.h>
int main(int argc, char**argv){
if ( printf("Hello, world!\n") ) {
}
return 0;
}
Finally, you are allowed to omit the return from main (which implicitly returns 0 in that case). So you get:
#include <stdio.h>
int main(int argc, char**argv){
if ( printf("Hello, world!\n") ) {
}
}
which is a complete, standard compliant version of Hello, world! without any semicolons.
Since the return type of printf is a number, and all the numbers are true which are not 0 and 0 is false, a number in an if statement can be evaluated. That's why the source code works. When you call a function in an evaluation the function must return the value before the evaluation happens, so printf does what it has to do, returns a number and the if evaluates it. That's what this source code does.
Since the operation is in the condition it doesn't need a semicolon (that's why it works without a semicolon).
You're essentially passing as the condition to "if" the output from printf.
printf returns an integer, the total number of characters written, so if let's say printf writes out the 25 characters then it returns 25 and your "if" statement is doing something like this...
if( printf( "blahblahblah"/* 25 characters */ ) ){}
if( 25 ){}
if( TRUE == 25 )
{
// do nothing
}
At the point when the "if" statement evaluates the condition, it executes whatever is in that condition. So when the "if" statement starts it's evaluation, printf gets executed, prints out the text and returns the number of characters to the "if" condition's check (the return value from printf). Then the "if" statement decides whether to run the TRUE or FALSE part. There is only an empty TRUE part, so it does nothing after having executed the printf statement and evaluated the return.
The if
statement will simply check if the expression has evaluated to non-zero value. So
if (printf("Hello World")) { }
is pretty much the same is
if (printf("Hello World") != 0) { }
which printf
needs to be called to evaluate the expression.