61

My friend says it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons. After minimal research I told her it was not possible. Is it possible?

dan1st
  • 12,568
  • 8
  • 34
  • 67
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

7 Answers7

199
#include <stdio.h>

int main() {
    switch (printf("Hello, world!\n")) {}
}

If your friend says "oh, you can't use switch either," then:

#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
74

I've been trying to find a "portable" way of stealing a semicolon from an include file. This works under 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_t to become my printf.

Another trick that worked was to #define away every standard stdint type such that stdint.h reduces to a bunch of semicolons.

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.

It seems like it should be possible to steal a semicolon cleanly from an include file. Can anyone improve on my attempt?

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
41

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"...)

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
20

it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons.

Easy. Note that C is case sensitive.

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

if is a keyword in C, IF is not.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
16

You could also workaround the limitation like

#define X i##f
#define Y whi##le
#define Z f##or
#define W swi##tch
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
15

What about:

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

ain't C cool :)

steabert
  • 6,540
  • 2
  • 26
  • 32
  • 1
    could you explain what it is leagal to insert a function call in another function parameter declaration block? I can hardly understand that. – Kuba Wyrostek Jul 26 '12 at 22:58
  • I would really love to know... :-) – Kuba Wyrostek Aug 26 '12 at 20:15
  • printf returns an int (a predictable one in this case too), which means that the full definition of the function, is int main(void * HAHA[12]) { } In order to find out that the magic number is 12, you must first execute the printf statement. – Clearer Dec 12 '14 at 22:16
  • Doesn't work: error C2057: expected constant expression (Microsoft C/C++ compiler 17) – ZhekaKozlov Jan 14 '15 at 12:46
  • @ZhekaKozlov: you need a proper C99 compiler. – Paul R Sep 08 '17 at 07:11
1

you can use switch statement to get your desire output,here is the code below

#include<stdio.h>

int main()
{
  switch(printf("hello world"))

return 0;
}

hope this will help you

Zishan
  • 613
  • 8
  • 12