C语言 没有分号和 IF/WHILE/FOR 语句的 C 语言中的 hello world
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4147836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hello world in C without semicolons and without IF/WHILE/FOR statements
提问by l--''''''---------''''''''''''
My friend says it's possible to write a C program that will print "hello world" without IF/WHILE/FORand without semicolons. After minimal research I told her it was not possible. Is it possible?
我的朋友说可以编写一个 C 程序,它可以在IF/WHILE/FOR没有分号和没有分号的情况下打印“hello world” 。经过最少的研究,我告诉她这是不可能的。是否可以?
回答by cdhowie
#include <stdio.h>
int main() {
switch (printf("Hello, world!\n")) {}
}
If your friend says "oh, you can't use switch either," then:
如果你的朋友说“哦,你也不能使用 switch”,那么:
#include <stdio.h>
int main(int argc, char *argv[printf("Hello, world!\n")]) {}
回答by Ben Hymanson
I've been trying to find a "portable" way of stealing a semicolon from an include file. This works under Linux:
我一直试图找到一种从包含文件中窃取分号的“便携”方式。这在 Linux 下有效:
int main(int ac, char **av)
{
#define typedef
#define uint8_t a[printf("hello world\n")]
#include <stdint.h>
}
This causes the one typedef unsigned char uint8_tto become my printf.
这导致它typedef unsigned char uint8_t成为我的 printf 。
Another trick that worked was to #defineaway every standard stdint type such that stdint.hreduces to a bunch of semicolons.
另一个有效的技巧是#define去掉每个标准的 stdint 类型,stdint.h从而减少到一堆分号。
Both of these fall flat on FreeBSD because it uses private intermediate types (like __uint8_t) which means that removing typedef fails in the quoted example and prevents me from successfully removing all non-semicolons in the other case.
这两个都在 FreeBSD 上失败,因为它使用私有中间类型(如__uint8_t),这意味着在引用的示例中删除 typedef 失败,并阻止我在其他情况下成功删除所有非分号。
It seems like it should be possible to steal a semicolon cleanly from an include file. Can anyone improve on my attempt?
似乎应该可以从包含文件中干净地窃取分号。任何人都可以改进我的尝试吗?
回答by Ben Hymanson
I'm torn about whether to suggest this because it hinges on the exact wording of the question, but:
我对是否提出这一建议感到困惑,因为它取决于问题的确切措辞,但是:
#error hello world
(if nothing else, perhaps it will stave off a followup "how do you print hello world without main"...)
(如果不出意外,也许它会避免后续“你如何在没有main”的情况下打印 hello world ......)
回答by JeremyP
it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons.
可以编写一个 C 程序,在没有 IF/WHILE/FOR 和没有分号的情况下打印“hello world”。
Easy. Note that C is case sensitive.
简单。请注意,C 区分大小写。
int main()
{
if (printf("Hello, World\n")){}
}
ifis a keyword in C, IFis not.
if是 C 中的关键字,IF不是。
回答by kennytm
You could also workaround the limitation like
你也可以解决这个限制,比如
#define X i##f
#define Y whi##le
#define Z f##or
#define W swi##tch
回答by steabert
What about:
关于什么:
#include <stdio.h>
int main(void *HAHA[printf("Hello world!\n")]) {}
ain't C cool :)
C 不是很酷 :)
回答by Zishan
you can use switch statement to get your desire output,here is the code below
您可以使用 switch 语句来获得您想要的输出,这是下面的代码
#include<stdio.h>
int main()
{
switch(printf("hello world"))
return 0;
}
hope this will help you
希望能帮到你

