For you C programmers! Real Good!

wolf550e

Golden Member
May 22, 2000
1,370
0
0
found it here

Here's a nice challange for people who THINK they know how to read other people's code...

It's a small C program. I know, I know, it doesn't look like it's gonna get compiled, but believe me, it does. Try to guess this program's output, then try to run it. You'll be amazed to see what it does!

Here's the program in a normal text file.


#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&amp;&amp;t==2?_<13?
main(2,_+1,&quot;%s %d %d\n&quot;):9:16:t<0?t<-72?main(_,t,
&quot;@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/&quot;)
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,&quot;%s&quot;):*a=='/'||main(0,main(-61,*a,
&quot;!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry&quot;),a+1);}

Ohh, an trust me, it works great. I just ran it on Borland C++ 3.1 for DOS. it is not harmful, i do not understand the code, and i want to know how the heck does it work.
i dont think it will work with Visual CPP 6 though, you need a standart C compiler. (or VC6 can do it? edit: yes it can!)
 

ggavinmoss

Diamond Member
Apr 20, 2001
4,798
1
0
Is this the one that prints the 12 days of Christmas? If not, there's another one of these that looks &quot;just like&quot; your post that does.

Fun stuff, bad style. :)

-geoff
 

wizzz

Senior member
Jul 20, 2000
267
0
0
how the hell does that sh!t print this out?
The code makes no sense.

On the first day of Christmas my true love gave to me
a partridge in a pear tree.

On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.

On the third day of Christmas my true love gave to me
three french hens, two turtle doves
and a partridge in a pear tree.

On the fourth day of Christmas my true love gave to me
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the fifth day of Christmas my true love gave to me
five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the sixth day of Christmas my true love gave to me
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the seventh day of Christmas my true love gave to me
seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eigth day of Christmas my true love gave to me
eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the ninth day of Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eleventh day of Christmas my true love gave to me
eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

 

gooch

Member
Oct 11, 1999
199
0
0
Talk about your ugly code. Interesting though. Recursive calls to main()... bad
 

wolf550e

Golden Member
May 22, 2000
1,370
0
0
yes, thats the output. and exactly my question, how does it work. c'mmon, any C gurus here?
i think it might be inline machine instructions, or the text is encrypted in some numeric form. what procedures does it use for output? how does it call the printf from stdio? (oh, and nice to hear M$ has full compatability with generic C)
 

ggavinmoss

Diamond Member
Apr 20, 2001
4,798
1
0
Well... the code DOES make sense, it's just sick. It just makes use of unorthodox code styling and variable naming, etc.

Look at the structure a bit... it's just a sick number of recursive calls to main (remember that main is usually called like so:

main (int argc, char **argv).

Since there's that third argument, a, it is typed as it was awhile ago -- after the function name and arguments (is that non-ANSI?).

And if you're even more curious, you can go through and find all '{' and '}' that aren't in strings so you can see what kind of scopes are set up.

The one thing I definitely can't determine by glancing at it is if there are any looping mechanisms that are hidden within, or if it's all recursion. <sigh> Well I don't care that much...

-geoff
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
you mean normal code doesn't usually look like that? damn what have I been making all these years..:confused:
 

wolf550e

Golden Member
May 22, 2000
1,370
0
0
Kranky: You're da Man!
10x a lot for finding this. i would have never figured that proggy out!

i'm sending it to my high school computer science teacher (well yes, she teches pascal but i know she knows C), to see how smart she is...