代码之家  ›  专栏  ›  技术社区  ›  Alex Gordon

hello world在C中没有分号,也没有IF/WHILE/FOR语句[关闭]

  •  60
  • Alex Gordon  · 技术社区  · 14 年前

    我朋友说可以写一个C程序,不需要 IF/WHILE/FOR 没有分号。经过最少的研究,我告诉她这是不可能的。有可能吗?

    7 回复  |  直到 10 年前
        1
  •  74
  •   Ben Jackson    14 年前

    我一直在试图找到一种从include文件中窃取分号的“可移植”方法。这在Linux下工作:

    int main(int ac, char **av)
    {
    #define typedef
    #define uint8_t a[printf("hello world\n")]
    #include <stdint.h>
    }
    

    typedef unsigned char uint8_t 成为我的印刷品。

    另一个有效的方法是 #define stdint.h 缩减为一堆分号。

    __uint8_t )这意味着删除typedef在引用的示例中失败,并阻止我在另一种情况下成功删除所有非分号。

    似乎应该可以从include文件中干净地窃取分号。有人能改进我的尝试吗?

        2
  •  198
  •   cdhowie    14 年前
    #include <stdio.h>
    
    int main() {
        switch (printf("Hello, world!\n")) {}
    }
    

    如果你的朋友说“哦,你也不能用开关”,那么:

    #include <stdio.h>
    
    int main(int argc, char *argv[printf("Hello, world!\n")]) {}
    
        3
  •  41
  •   Ben Jackson    14 年前

    #error hello world
    

    (如果没有其他内容,也许可以避免后续的“how do you print hello world without main

        4
  •  19
  •   JeremyP    14 年前

    可以编写一个C程序,它将打印“hello world”,不带IF/WHILE/FOR和分号。

    容易的。注意C是区分大小写的。

    int main()
    {
        if (printf("Hello, World\n")){}
    }
    

    if IF 不是。

        5
  •  16
  •   kennytm    14 年前

    你也可以解决这个限制,比如

    #define X i##f
    #define Y whi##le
    #define Z f##or
    #define W swi##tch
    
        6
  •  15
  •   steabert    14 年前

    #include <stdio.h>
    int main(void *HAHA[printf("Hello world!\n")]) {}
    

    很酷:)

        7
  •  2
  •   Zishan    11 年前

    您可以使用switch语句获得您想要的输出,下面是代码

    #include<stdio.h>
    
    int main()
    {
      switch(printf("hello world"))
    
    return 0;
    }