views:

2585

answers:

8

I recently heard this was used as an interview question. I suspect there is a very simple answer; I must be over-thinking it.

Can you write Hello World in C without using any semi-colons? If so, how?

+87  A: 
#include<stdio.h>
main()
{
   if(printf("Hello World \n"))
   {
   }
}
Robert Harvey
So I built this as a win32 console application in VS2005; if it's foo.cpp, then I get a warning about implicit int. If I rename to foo.c, it works just fine, but I too wonder if this is only because I'm using the C++ compiler?
jeffamaphone
@jeffamaphone: C99 would require 'int main()'; C++ already does. C89 did not require that, and MSVC really only supports C89.
Jonathan Leffler
@Jonathan: Good to know.
jeffamaphone
ha, I wouldn't have thought of it, despite the fact I write functions inside of conditional statements routinely. well done
Tom Dignan
+61  A: 
#error "Hello, World"
Nick D
I like that, but it prints at compile time, not run time. :)
jeffamaphone
@jeffamaphone, yes, I cheated, but you didn't specify it ;-)
Nick D
Compile and run time produce the same result, so +1.
Tim Post
+1: Just for being a smartass!
Donal Fellows
+66  A: 

Any ANSI C solution with printf would require #include <stdio.h> or an explicit "manual" declaration of printf to be correct (the latter would need a semicolon though), since without the declaration of printf the behavior of the program is undefined.

One can also argue that by doing #include <stdio.h> you are implicitly introducing semicolons into your code :)

If you care to make it more compact, and also discourage any potential #include <stdio.h> protesters, use puts instead of printf

main() {
  if (puts("Hello World")) {}
}

Of course, this is only valid in C89/90.

AndreyT
+1 because it completely lack semicolons
SztupY
I like this one too; good point about the `#include`s.
jeffamaphone
+1, but to pick a nit `puts` will append a newline on its own, no need to include another in the literal.
jtb
@jtb: Good point. Corrected. Thanks.
AndreyT
+3  A: 
int
main (void)
{
  while (puts ("Hello World!") && 0)
    {}
}

Unfortunately, that omits the return statement. Some compilers will issue a warning, but it will still compile. The only one that REALLY works and is completely valid at the time of this writing (despite not producing a binary and lacking a main function in some form) is the one using #error "Hello World". Of course, that's if you're shooting for full C89/C90 compatibility with C99 as well. :P

Dustin
susmits
@susmits: The return value of `puts` is non-zero, so the loop would go on forever otherwise.
GMan
And theoretically `puts` is better than `printf` in the regard that `printf` needs to scan for formatting instructions in the string before actually outputting any information whereas `puts` simply prints the string to `stdout`.
Dustin
+21  A: 

And here's a semicolonless quine, inspired by this question:

#include <stdio.h>
main(char* a){if(a="#include <stdio.h>%cmain(char* a){if(a=%c%s%c){if(printf(a,10,34,a,34)){}}}"){if(printf(a,10,34,a,34)){}}}

Compile with gcc.

KirarinSnow
+9  A: 

To be compliant, it should return a status code of 0 to the OS.

I like this version:

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
   if (printf("Hello World\n"), exit(0), 0) { } 
}

(answer edited based on comments. Tested under VS 2010 as Win32 Console App)

abelenky
compile error: `4: error: void value not ignored as it ought to be`
N 1.1
What compiler is that? I've tried it on several myself, and it worked fine.
abelenky
A C99 program returns 0 to the OS automatically. A C89/90 returns undefined value, but it doesn't make the program non-compliant.
AndreyT
@abelenky: Your code is not valid because `exit` is a `void` function. The only reason it might compile (as it apparently did in your case) is that you failed to include the declaration of `exit` (it is in `stdlib.h`) and the compiler assumed that it returns `int`. Needless to say, in such case the behavior of your program is undefined, even if it compiles. In order to make it valid you need to do `if (printf(...), exit(0), 0)` for example, as GMan did in his deleted answer. But what you have now is plainly invalid.
AndreyT
Indeed, this is what I have as my deleted answer. I deleted it because there is no need to try to return anything in C99. And if you are using C89/90, we might as well go with @AndreyT's answer, which I personally find to be the best answer.
GMan
+5  A: 

file name:

void main(){if(puts("Hello, World!")){}}

file contents:

A

compile with:

-istdio.h -DA=__FILE__
Time Machine
compile error: File truncated
N 1.1
Seriously? It works here =\
Time Machine
Can't create a file with " in the filename on Windows.
jeffamaphone
@jeffamaphone there's always Mac OS X, which even allows `/`es in filenames.
Time Machine
Yeah, but I'm not gonna buy a Mac just for this.
jeffamaphone
Yeah you do! =D
Time Machine
A: 

Can you write Hello World in C without using any semi-colons? If so, how?

Sought out answer already provided, but given the question...

[sarcasm]

int main()
{
   // Hello World
}

[/sarcasm]

I think a lot of interview questions are often very poorly worded. I was once asked, "How many bits are required to represent the number, 32?" I had to ask, "for what range, and is this for integers only?" The interviewer answered that it was for integers and starting from zero, so I said 6. Then he said that was wrong and the correct answer was 5. I told him he was wrong as that would assume the range [1, 32], not [0, 32] which would have 33 unique values and would require 6 bits. I got the job but the interview was silly. Sometimes it seems like they just came up with the question on the spot.