How does this if statement work to avoid using a s

2020-07-22 16:35发布

It has been a popular question about 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")){}

标签: c
7条回答
We Are One
2楼-- · 2020-07-22 16:40

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(void) {
  int i = printf("Hello, world!\n");
  printf("%d\n",i);
  return 0;
}

#include <stdio.h>
int main(void) {
  if ( 1 ) {
    printf("condition evaluated as true\n");
  } else {
    printf("condition evaluated as false\n");
  }
  return 0;
}

#include <stdio.h>
int main(void) {
  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(void) {
  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(void) {
  if ( printf("Hello, world!\n") ) {
  }
}

which is a complete, standard compliant version of Hello, world! without any semicolons.

查看更多
beautiful°
3楼-- · 2020-07-22 16:43

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.

查看更多
戒情不戒烟
4楼-- · 2020-07-22 16:47

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.

查看更多
对你真心纯属浪费
5楼-- · 2020-07-22 17:00

You have to put a semicolon anyhow, just after the if statement, or you have to put an empty block after it.

if(printf("hello world"))
    ;

or

if(printf("hello world")) {}

EDIT: I was sure that in the question there wasn't the empty block... I must have read it wrong, or it has been ninja-edited.

It works because printf is a normal function, returning the number of characters printed (as you can clearly see from its documentation); the if statement obviously evaluates the expression, thus calling the function (which incidentally prints the string on the screen).

查看更多
虎瘦雄心在
6楼-- · 2020-07-22 17:01

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.

查看更多
做自己的国王
7楼-- · 2020-07-22 17:03

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.

查看更多
登录 后发表回答