how this program works

F

free2cric

Hi,
FOllowing is a program which is written by someone else.
Its output is a wonderful poem. I dont understand how it works really.
can anyone tell.
thanks
cric

#include <stdio.h>
main(int t,char _,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
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@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;# ){n\
l]!/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' ')# }'+}##(!!/") :t<-50?_==*a
?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
 
A

aurgathor

Hehhehe, this is like an entry from an obfuscation
contest. Usually, there are some hints there.

BTW, I did a copy'n'paste into BC++ 5.02, and it
doesn't compile.
 
T

Tom St Denis

aurgathor said:
Hehhehe, this is like an entry from an obfuscation
contest. Usually, there are some hints there.

BTW, I did a copy'n'paste into BC++ 5.02, and it
doesn't compile.

GNU indent didn't like it neither.

I think it just isn't valid C code...

Tom
 
A

aurgathor

Tom St Denis said:
GNU indent didn't like it neither.

I think it just isn't valid C code...
Could be a code written for some old, pre-ansi
compiler. Of course, if the original poster was
able to compile and run this code, s/he should've
posted the compiler and instructions.
 
R

Richard Bos

Tom St Denis said:
GNU indent didn't like it neither.

I think it just isn't valid C code...

With a declaration like main(int t,char _,char *a)? I think it's safe to
say that it's nowhere near valid, let alone portable, C code.

Richard
 
S

Steven

free2cric said:
Hi,
FOllowing is a program which is written by someone else.
Its output is a wonderful poem. I dont understand how it works really.
can anyone tell.
thanks
cric

Yes, I remeber this program. It is valid and it compiles fine with
GCC, even though it generates plenty of warnings. I *think* it works
by having a obscure string, do some operations on it to make it into
the lyrics, name it a obscure name, delete all the white space, print
the different substring of the string, and use recursion to print the
whole thing.

This program uses recursion to print one of the most famous Christmas
songs, "Twelve Days of Christmas".

The output of the program is:

"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
sevean 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 eig hth day of Christmas my true love gave to me
eight maids a-milking, sevean 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, sevean 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, sevean 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, sevean 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, sevean 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."

You have to admit, whoever wrote the program was pretty good.
 
C

CBFalconer

Tom said:
GNU indent didn't like it neither.

I think it just isn't valid C code...

[1] c:\c\junk>cc obfusc.c
obfusc.c:3: warning: return type defaults to `int'
obfusc.c:3: warning: second argument of `main' should be `char **'
obfusc.c:3: warning: third argument of `main' should probably be
`char **'
obfusc.c: In function `main':
obfusc.c:6: warning: passing arg 3 of `main' discards qualifiers
from pointer target type
obfusc.c:7:3: warning: multi-line string literals are deprecated
obfusc.c:7:3: warning: unknown escape sequence: '\040'
obfusc.c:7: warning: passing arg 3 of `main' discards qualifiers
from pointer target type
obfusc.c:14: warning: passing arg 3 of `main' discards qualifiers
from pointer target type
obfusc.c:14: warning: passing arg 3 of `main' discards qualifiers
from pointer target type
 
F

free2cric

Yes its from some contest and a winner I suppose.
I compiled it into .exe using MS VC++ 6.0.
What is obscure string.
Can someone tell in more detail. Its interesting. We can all get benefit of.
thanks cric.
 
M

Mike Wahler

free2cric said:
Yes its from some contest and a winner I suppose.
I compiled it into .exe using MS VC++ 6.0.

My VC++6.0(SP6) rejected it (but perhaps it got
distorted by my newsreader).
What is obscure string.

It's in the code. Actually there are two strings
(not counting the two 'printf()' format strings):

"@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;# ){n\
l]!/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' ')# }'+}##(!!/"

and

"!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"
Can someone tell in more detail.

Tell what.
Its interesting.

Perhaps it is to some. I suggest that if you want to
figure out how it works, do one or both of:

1. Work through it with pencil and paper.

2. Watch it with a debugger. (this might involve obtaining
an implementation that doesn't reject the language
violations.)
We can all get benefit of.

Some might, others not. It could also cause some
to believe it's valid code.

BTW please don't top-post in comp.lang.c. Thank you.

-Mike
 
A

aurgathor

free2cric said:
Yes its from some contest and a winner I suppose.
I compiled it into .exe using MS VC++ 6.0.
What is obscure string.
Can someone tell in more detail. Its interesting. We can all get benefit of.
thanks cric.

I just tried VC++ 6.0 and no got even more errors than with BC++ 5.02.
In any case, if you look at the code, it's recursively calling main()
depending
on "t", end at the end, it does a putchar(). It shouldn't take more than a
few
hours with a debugger to figure out "exactly" how it works, but I kinda
doubt that anyone will do it for you. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top