实际上如何做到这一点的if语句工作 [英] How actually does this if statement work

查看:87
本文介绍了实际上如何做到这一点的if语句工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个受欢迎的疑问,如何打印的hello world不使用semicolon.I知道很多codeS,而是因为我无法身后it.Please帮助我知道它是如何打印获得逻辑这个听起来不可思议。

 如果(的printf(世界你好)){}


解决方案

有关分号位是一点点我比你更聪明误导。

然而,当你得到这个你就会知道一些关于C;

下面是一系列可帮助计划。编译并运行各一个,再想想他们做什么,他们从之前来到的人有什么不同:

 的#include<&stdio.h中GT;
INT主(INT ARGC,字符** argv的){
  INT I =的printf(你好,世界\\ n!);
  的printf(%d个\\ N,I);
  返回0;
}


 的#include<&stdio.h中GT;
INT主(INT ARGC,字符** argv的){
  如果(1){
    的printf(条件判断为真\\ n);
  }其他{
    的printf(条件评估为假\\ n);
  }
  返回0;
}


 的#include<&stdio.h中GT;
INT主(INT ARGC,字符** argv的){
  如果(的printf(你好,世界!\\ n)){
    的printf(条件判断为真\\ n);
  }其他{
    的printf(条件评估为假\\ n);
  }
  返回0;
}


 的#include<&stdio.h中GT;
INT主(INT ARGC,字符** argv的){
  如果(的printf(你好,世界!\\ n)){
  }
  返回0;
}


最后,你被允许省略主要回报(这隐含在这种情况下返回0)。所以,你得到:

 的#include<&stdio.h中GT;
INT主(INT ARGC,字符** argv的){
  如果(的printf(你好,世界!\\ n)){
  }
}

这是为你好的一个完整的,符合标准的版本,世界!没有任何分号。

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")){}

解决方案

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.

这篇关于实际上如何做到这一点的if语句工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆