• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can someone explain to me how this can be a valid C program?

Special K

Diamond Member
I was browsing a C reference website and they were pointing out that C has very flexible syntax rules, and gave the code below as an example.

Can someone please explain to me how this is a valid program? I can't make any sense of it, although it compiles fine. It outputs the words to "The Twelve Days of Christmas" to the console.

EDIT:

The code is found at this link:

WTF is this code
 
I would but it looks like gibberish to me. Which is why I'll advise you NOT to find out why it's valid. Don't code in that way or you'll make more headaches for yourself than you care to handle. Do yourself a favor and learn to write clean, readable code.

Well it's still unreadable so my "don't code like that and don't learn from that" still stands.
 
It's beautiful! Just add some comments to it, like

t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+ // snurfle teh sniggly shangtrong

;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") // wiggle the widget

i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);} // psynothecize 66th squibble

 
There is a whole little programming subculture for writing obfuscated code. The entire point of obfuscated code is that you can't tell what it does. There used to be an annual obfuscated perl contest.

The reason you can't tell what the hell that code is supposed to do is because the author did a good job.

The following code will print out "Just another perl hacker":

`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Originally posted by: notfred
There is a whole little programming subculture for writing obfuscated code. The entire point of obfuscated code is that you can't tell what it does. There used to be an annual obfuscated perl contest.

The reason you can't tell what the hell that code is supposed to do is because the author did a good job.

The following code will print out "Just another perl hacker":

`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

obfuscated perl? that's kinda redundant 😉
 
Originally posted by: dighn
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. 😕

that's because you compiled it as C++. not all C code is valid C++

No, the first time I posted it it was incorrect because fusetalk wasn't displaying it correctly.

So can anyone rewrite this code to make it clear to read? I have absolutely no idea how you could get the text to "The Twelve Days of Christmas" out of that.

 
Originally posted by: dighn
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. 😕

that's because you compiled it as C++. not all C code is valid C++

No, the first time I posted it it was incorrect because fusetalk wasn't displaying it correctly.

So can anyone rewrite this code to make it clear to read? I have absolutely no idea how you could get the text to "The Twelve Days of Christmas" out of that.

 
Originally posted by: Special K
So can anyone rewrite this code to make it clear to read?
Probably not. That would completely defeat the purpose of why it was written. Why do you need to understand it anyway?

And yeah, I was thinking "obfuscated perl" was sort of the opposite of an oxymoron, like wet water or something 😛
 
Originally posted by: Special K
Originally posted by: dighn
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. 😕

that's because you compiled it as C++. not all C code is valid C++

No, the first time I posted it it was incorrect because fusetalk wasn't displaying it correctly.

So can anyone rewrite this code to make it clear to read? I have absolutely no idea how you could get the text to "The Twelve Days of Christmas" out of that.

either way though, I don't think this part: "main(t,_,a) char *a; {return" compiles as C++
 
Right, it worked when I compiled it as a .c file but not as a .cpp one.

What does the underscore in main(t,_,a) mean? What are t and a? And how can there be a char declaration before the starting brace for the function? What's the exclamation after the return mean?
 
Hah, I found that if you deleted one character (somewhere in the code) you can make the whole thing crash after printing "On the ". Can anyone live up to the challenge? 😉
 
Originally posted by: xtknight
Right, it worked when I compiled it as a .c file but not as a .cpp one.

What does the underscore in main(t,_,a) mean? What are t and a? And how can there be a char declaration before the starting brace for the function? What's the exclamation after the return mean?

Yea I wondered those things as well.

Can't anyone rewrite this into a form that makes sense?😉

 
Can't anyone rewrite this into a form that makes sense?

Most people have a hard enough time deciphering someone's code when they're not trying to obfuscate it, your best bet would be to find someone who knows C like the back of their hand, the gcc mailinglists might be a good place to start.
 
Back
Top