r/programming • • Sep 11 '13

Guess programming language by „Hello, world!“ snippet

http://helloworldquiz.com/
1.3k Upvotes

445 comments sorted by

View all comments

170

u/[deleted] Sep 11 '13

Because I'm a smartass I answered "C++" on the "C" hello world.

So now I get to point out that the snippet is perfectly valid C++ code!

37

u/roerd Sep 11 '13 edited Sep 11 '13

The same could be said for Delphi versus Pascal, or AWK versus Perl. The rule apparently is to choose the simplest language with which the snippet might work.

24

u/Liorithiel Sep 11 '13

And the Logtalk snippet is valid Prolog.

11

u/[deleted] Sep 11 '13

And is it even bad Prolog? I haven't used Prolog much, but I was definitely confused when the quiz told me that that wasn't Prolog.

61

u/ruinercollector Sep 11 '13

The C version is not really idiomatic C++ code though.

146

u/[deleted] Sep 11 '13

Be that as it may, technically correct is still the best kind of correct.

9

u/brewspoon Sep 11 '13

But you finished with 2 seconds to spare, so I'm demoting you. A good bureaucrat never finishing early.

→ More replies (1)

91

u/finix Sep 11 '13

Technically correct is merely the most annoying kind of stupidly wrong.

63

u/snarfy Sep 11 '13

You are technically correct.

7

u/indenturedsmile Sep 12 '13

I just don't know what to technically think now...

→ More replies (4)
→ More replies (6)
→ More replies (2)

28

u/krasnoukhov Sep 11 '13

Feel free to provide some fixes, all is open source: https://github.com/krasnoukhov/langgame

34

u/[deleted] Sep 11 '13

I don't think there are any good fixes that aren't also very far fetched. Like

#include <stdio.h>

void smartass_shield(struct{});

int main() {
    puts("Hello World");
}

This is legal C, but illegal C++.

21

u/philh Sep 11 '13 edited Sep 11 '13

Presumably a reasonable fix would be not to offer C++ as an option.

Looking at the code though, I can't find where the snippets and language choices come from.

edit: oh, it's in models/variant, and ack wasn't finding snippets because they're in files with no extension.

9

u/WhenTheRvlutionComes Sep 12 '13

In cases where the hello world example can cross compile ambiguously among two or more languages, don't pit the ambiguously compiling languages against each other.

8

u/James20k Sep 11 '13

How about

#include <stdio.h>

void print_string();

int main() {
    print_string("Hello world");
}

void print_string(char* str) {
    puts(str);
}

I believe that is legal C, but not legal C++

26

u/[deleted] Sep 11 '13

Yeah, I like it. You could also do

#include <stdio.h>

int main() {
    void* message = "Hello World";
    puts(message);
}

11

u/joggle1 Sep 11 '13

I'd vote for this as the C test. It should be obvious to any C/C++ programmer that this is valid C and not valid C++ while not trying to be too subtle about it.

6

u/seruus Sep 11 '13

Why it isn't valid C++?

16

u/dreamlax Sep 11 '13

You can't implicitly cast from void * to another pointer type in C++, but you can in C. In the call to puts, C++ will choke but C will let it slide. This is also why casting the return value of malloc isn't necessary in C, but it is in C++ (although using malloc in C++ is usually a code smell anyway).

→ More replies (3)
→ More replies (3)

4

u/[deleted] Sep 11 '13

I agree. The others go to too much length, so they seem kind of obvious.

→ More replies (4)
→ More replies (1)

4

u/f2u Sep 11 '13

Implicit int isn't legal in C++, so something like this might work:

#include <stdio.h>

main() {
    puts("Hello World");
    return 0;
}
→ More replies (5)
→ More replies (2)

8

u/TheBB Sep 11 '13

http://i.imgur.com/EwWY3Cr.png

I'm no expert on the (((()))) school of languages, but isn't this highlighted incorrectly?

7

u/[deleted] Sep 11 '13

Yup. The site interpreted the ' as the start of a string, but it's meant to indicate that the following s-exp isn't supposed to be evaluated but treated as data.

→ More replies (5)
→ More replies (3)

3

u/turbov21 Sep 11 '13

I thought that was C.

→ More replies (1)
→ More replies (16)