tags:

views:

483

answers:

7

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")){}
+7  A: 
Matteo Italia
+5  A: 

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.

ChrisM
+6  A: 

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.

Tumas
how does the if statment prints it? it should just check wheather the condition is true or false which in this case is true and it should do nothing
fahad
The `if` statement doesn't print it--the `printf()` call does. In order to get the truth value of the condition (i.e. the return value of `printf()`), `printf()` must be called.
Jonathan Grynspan
@fahad: You're close to getting it. How does the compiler check if the condition is true or false when the condition is a function and not just a number?
dmckee
@dmckee: You mean to find the value of the condition it executes the function.But still why would it ever print it?
fahad
The condition inside if statement needs to evaluate to either true or false. And how could that happen? printf() needs to be called. Please follow dmckee's answer
Tumas
Oh got it...! It evaluates the function and actually the evaluation prints it on the screen ..thanks a million.
fahad
Heck, it's not that difficult! *if* calls *printf* (because it's evaluationg the expression to determine if it's true or false); so, printf runs, prints the text (because it's what printf do) and returns the number of chars printed (as described in the documentation). The return value is checked by if to see if it has to run the associated block, which is of no interest here because it's empty. ---EDIT--- Oh, finally you got it. :)
Matteo Italia
hahhahhahahaha finally
fahad
+25  A: 

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.

dmckee
Well done, this is the right approach. Sadly, SO doesn't incentivate it, and I too tend to provide direct answers even to newbies who would need more a guidance.
Matteo Italia
thank you buddy
fahad
+1 for the step-by-step explanation.
Péter Török
@dmckee: You should have explained, why he may leave out the return statement ;-)
Jens Gustedt
@Jens: You are right. My first version didn't have them, then I added them for good style without even thinking about the problem at hand. D'oh!
dmckee
It looks to me like your second and third have superfluous `i`s.
David Thornley
@David: They have. Cut-n-paste. Thanks.
dmckee
+1  A: 

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.

Lajos Arpad
+1  A: 

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.

Allbite
why is there a need of {} at all ?
fahad
@fahad: You don't need a {} block after an `if()`, but if you don't use it the statement that follows must end in a semicolon, which your fried has challenged you o avoid.
dmckee
I believe that `TRUE == 25` would be evaluated to false, but it is pseudo code so I will forgive you ;-).
tia
A: 

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.

tia