hey C[++] gurus! What does this code do...

Started by Darren Dirt, May 23, 2012, 12:51:05 PM

Previous topic - Next topic

Darren Dirt

_____________________

Strive for progress. Not perfection.
_____________________

Lazybones

Quote from: Darren Dirt on May 23, 2012, 12:51:05 PM
found via http://stackoverflow.com/questions/235848/most-astonishing-violation-of-the-principle-of-least-astonishment

QuoteThis question was removed from Stack Overflow for reasons of moderation. Please refer to the FAQ for possible explanations why a question might be removed.

Thorin

At first glance I thought I understood it.
At second glance, it looked a little complicated.
At third glance, MADNESS
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Check the accepted answer: http://meta.stackoverflow.com/questions/27112/amusing-404-page-not-found-images-for-trilogy-sites

AND the comment (explains why this is on the 404 page for Stack Overflow) :D

EDIT: (hint: HTTP 404)
By Grabthar's Hammer

Thorin

Ah yes, those aren't #defines, as there's incorrect spacing in all three of them.  They're just all commented out (as is line 3).
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Tom

Quote from: Thorin on May 23, 2012, 01:54:01 PM
Ah yes, those aren't #defines, as there's incorrect spacing in all three of them.  They're just all commented out (as is line 3).
Nope. You're allowed to have spaces before the command with the C preprocessor. So long as the text that follows is a cpp command. Otherwise it is just a comment of sorts.
<Zapata Prime> I smell Stanley... And he smells good!!!

Thorin

#<space>define
#<space><space><space>define

are not valid because there's a space between the # and "define".

#define/*>.@*/exit()

is not valid because there's no identifier

I'm not saying this won't compile, I'm saying lines 1, 2, 3, and 5 have no impact on what this code does.
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Did anyone actually follow this link: http://meta.stackoverflow.com/a/27846

'cause seriously Perl/Python/Ruby/C/Befunge/BF

NOT C++
By Grabthar's Hammer

Tom

Quote from: Thorin on May 23, 2012, 02:22:54 PM
#<space>define
#<space><space><space>define

are not valid because there's a space between the # and "define".

#define/*>.@*/exit()

is not valid because there's no identifier

I'm not saying this won't compile, I'm saying lines 1, 2, 3, and 5 have no impact on what this code does.
Again, not true. You can have spaces between the # and the command name. It's 100% valid at least on GCC, and I assume VC++ as well.

Quote from: Mr. Analog on May 23, 2012, 02:29:52 PM
Did anyone actually follow this link: http://meta.stackoverflow.com/a/27846

'cause seriously Perl/Python/Ruby/C/Befunge/BF

NOT C++
C++ is mostly a superset of C. So in this case, same difference. Both use the same preprocessor.
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

True, but I reckon Thorin is used to seeing MS Visual C++ not console C++

anywhoo FOLLOW DA LIMK

By Grabthar's Hammer

Tom

Quote from: Mr. Analog on May 23, 2012, 02:43:39 PM
True, but I reckon Thorin is used to seeing MS Visual C++ not console C++

anywhoo FOLLOW DA LIMK


I did ;)
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

cool cool :D

Also I just tried running this in C++ and got a compile error, but it went like bananas in C, and I got the expected output :D
By Grabthar's Hammer

Tom

Quote from: Mr. Analog on May 23, 2012, 02:51:24 PM
cool cool :D

Also I just tried running this in C++ and got a compile error, but it went like bananas in C, and I got the expected output :D
Yeah, C++ probably doesn't like the lack of "int" before main, and the lack of parameters in main as well. () in C++ is the same as (void) in C. () in C is "unspecified" meaning it can take any number of parameters.
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

Yep, t'was a trip down memory lane... I haven't touched C++ in a while...

Also, best 404 message EVAR
By Grabthar's Hammer

Lazybones

Just for fun (since I don't have have a c compiler installed)

http://codepad.org/e1L959z5

Thorin

I clicked through the first link you posted and read the accepted answer (and a bunch of others,  interesting topic).  I've also now clicked through your second link, not realizing it was the accepted answer.

I don't believe I said anything about C++?

Also

Quote from: Tom on May 23, 2012, 02:39:59 PM
Again, not true. You can have spaces between the # and the command name. It's 100% valid at least on GCC, and I assume VC++ as well.

if that is true then I learned something new today.

Every C (not C++) reference I read for preprocessor directives in the last 15 minutes says nothing about allowing spaces between # and define, neither whether it is allowed or not.
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Well I assumed you were talking about C++ because very often when you see stuff defined there's no space "#define blah" you mentioned the space so I jumped to conclusions (on my special Jump to Conclusions Mat)
By Grabthar's Hammer

Thorin

Nice Lazy.  So in C the #defines are accepted and used, in Perl they're simply commented out.

Had to look up putchar (http://www.cplusplus.com/reference/clibrary/cstdio/putchar/) and then ASCII codes (http://ascii.cl/htmlcodes.htm) to see what those first two #defines do.

Still.  Barn.  Behind.  Beaten.

And I should borrow that mat of your sometime, I do it to :)
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Lazybones


Mr. Analog

Well, now I know Thorin reads my posts all the way through LOL
By Grabthar's Hammer

Darren Dirt

So I didn't read the SO comments/discussion, but I did visit the http://codepad.org/ link and saw the output.

Whoever offered that as a suggestion for the 404 for the above-average-intelligence community of Stack Overflow: Way to make the over-thinkers of the world feel pretty dumb. Well played.

_____________________

Strive for progress. Not perfection.
_____________________

Lazybones

Quote from: Mr. Analog on May 23, 2012, 03:36:44 PM
Well, now I know Thorin reads my posts all the way through LOL

Ok, I get it now.

Mr. Analog

Quote from: Darren Dirt on May 23, 2012, 03:38:16 PM
So I didn't read the SO comments/discussion, but I did visit the http://codepad.org/ link and saw the output.

Whoever offered that as a suggestion for the 404 for the above-average-intelligence community of Stack Overflow: Way to make the over-thinkers of the world feel pretty dumb. Well played.

Yeah but think about all the laughs shared by programmers :D (and try not to think of the furrowed brows of the non-programmers)

Quote from: Lazybones on May 23, 2012, 03:38:52 PMOk, I get it now.

;)
By Grabthar's Hammer

Darren Dirt

Also, it *appears* "polyglot" which in theory means the output for C is the output for Perl or Python or whatever the other language is -- although it might depend on compilers or whatnot, although now it's clear the intention is to be the same "destination", just gets there via a different "route".

http://en.wikipedia.org/wiki/Polyglot_%28computing%29
_____________________

Strive for progress. Not perfection.
_____________________

Thorin

Quote from: Mr. Analog on May 23, 2012, 03:36:44 PM
Well, now I know Thorin reads my posts all the way through LOL

Yes I do.  I'm compelled to.  Not fully reading a post feels ... wrong.

What OCD?

Anyway, my first introduction to the word "polyglot" was as a kid when I was reading the OD&D Masters ruleset and they were talking about the different paths to demigodhood.  It feels weird using that word to describe a bunch of text interpreted as code for multiple compilers.
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Funny you mention that I was thinking about the first time I found out that all Palladium RPGs were compatible with each other with some minor tweaking.

By Grabthar's Hammer