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

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
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
 

imported_electron

Senior member
Nov 6, 2005
427
0
0
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.
 

professor1942

Senior member
Dec 22, 2005
509
0
0
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

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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":

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

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. :confused:

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

dighn

Lifer
Aug 12, 2001
22,820
4
81
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 ;)
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Originally posted by: dighn
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. :confused:

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.

 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Originally posted by: dighn
Originally posted by: xtknight
Doesn't compile for me in VC6 (nor VS2005). Not that I'd expect it to. :confused:

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.

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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 :p
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
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. :confused:

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++
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
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?
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
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? ;)
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
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?;)

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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.