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?

- 12,568
- 8
- 34
- 67

- 57,446
- 287
- 670
- 1,062
-
4Related: http://stackoverflow.com/questions/2754493/hello-world-in-c-with-no-semi-colons – kennytm Nov 10 '10 at 18:40
-
35Why would you want to? If your keyboard is missing a semicolon you have deeper problems. – John Smith Nov 10 '10 at 18:41
-
4Sounds like your friend revels in those obfuscated C and PERL competitions? – winwaed Nov 10 '10 at 18:48
-
2@John: For the same reason people write entries for the obfuscated C contest, or write Perl poetry, or do Sudoku. – cdhowie Nov 10 '10 at 18:48
-
3If you have a missing key you can `cat` some random C file and cut-and-paste the `;`. – Ben Jackson Nov 10 '10 at 18:48
-
Can anyone help print it with #PRAGMA? – Nov 11 '10 at 03:11
-
@KennyTM, the link is 404 now. – Volker Stolz Nov 27 '12 at 09:41
-
2Now that [codegolf.se] is up, this kind of question should be posted there instead. – Andrew T. Oct 23 '14 at 09:43
7 Answers
#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")]) {}

- 158,093
- 24
- 286
- 300
-
3
-
24
-
@iam: [Rep cap](http://meta.stackexchange.com/questions/7237/how-does-reputation-work). – kennytm Nov 10 '10 at 19:50
-
-
7This is why I don't like C (or C++ for that matter). How on earth is that legal? :-S – Alxandr Nov 14 '10 at 05:01
-
@Alxandr `printf` returns an integer, so this fixes the length on *argv to the return value by printf. – Tyzoid Aug 27 '13 at 17:28
-
@cdhowie Doesn't work: error C2057: expected constant expression (Microsoft C/C++ compiler 17) – ZhekaKozlov Jan 14 '15 at 12:45
-
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?

- 90,079
- 9
- 98
- 150
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
"...)

- 90,079
- 9
- 98
- 150
-
1
-
2The main issue with this technique is that it doesn't actually produce a runnable program. However, it is certainly clever. And a clever solution is never a bad solution. – cdhowie Nov 10 '10 at 19:11
-
-
1
-
Doesn't work on all platforms, but... `#pragma message("Hello World!")` Plus, it'll create a runnable program. Just the program won't do anything :). – yash101 Jul 28 '16 at 20:05
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.

- 84,577
- 15
- 123
- 161
-
Why the down vote? I complied to the letter, to the very case of the question. It seems a bit harsh when the question itself is one of those useless "how can we legally abuse C" questions. – JeremyP Nov 11 '10 at 14:28
-
1@JeremyP I didn't vote other way, but nobody likes a rules lawyer. – Dan Is Fiddling By Firelight Nov 16 '10 at 21:07
-
8@Dan Neely: What is a rules lawyer? Let's be honest, the original question is not exactly a serious programming question, so why do people take a tongue in cheek answer seriously and down vote it? – JeremyP Nov 17 '10 at 09:22
-
-
-
@AbuisX IF, WHILE, FOR are nothing to do with C unless you define them. – JeremyP Jul 04 '16 at 09:52
You could also workaround the limitation like
#define X i##f
#define Y whi##le
#define Z f##or
#define W swi##tch

- 510,854
- 105
- 1,084
- 1,005
What about:
#include <stdio.h>
int main(void *HAHA[printf("Hello world!\n")]) {}
ain't C cool :)

- 6,540
- 2
- 26
- 32
-
1could 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
-
-
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
-
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

- 613
- 8
- 12