-
Complicated Hello World
Here's an extrememly complex hello world app made by Bruce Holloway that boggles my mind. Aside from the intense use of macros and character math, the fact that he calls main recursively goes against everything I've every learned about C/C++. The definition is also odd, but I guess it sees m1 and s as argc and args (yes, no?). This was written in 1986, so perhaps the rules have changed.
If anybody can give their insight on those points, I'd like that. If not, this is simply for kicks and giggles! I'll post the code as well as the link to where I found it.
Code:
#include "stdio.h"
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
main(m1,s) char *s; {
int a,b,c,d,o[k],n=(int)s;
if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
else switch(m1-=h){
case f:
a=(b=(c=(d=g)<<g)<'<g)<<g;
return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
case h:
for(a=f;a=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
return(o[b-g]%n+k-h);
default:
if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
}
}
http://www2.latech.edu/~acm/helloworld/c.html
-
seems pretty natural. i mean, thats how i would go about writting hello world :p
-
It won't compile on my computer. I tried to fix it, but I don't know where he is coming from.
Code:
a = (b = (c = (d = g) << g) < '<g)<<g;
the ' in there
Code:
for(a=f;a=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
the for is missing a semicolon I added it and then it segfaulted:(
-
Yes, I forgot to mention that. I removed that apostrophe, and added a semicolon in that for statement. It also breaks on me because s eventually becomes NULL, and it get's ........y when it dereferences it.
BUT!!! It compiles, and I don't know why.
-
You are no longer allowed to call main recursively. If you take a look at the macros, you can simplify them somewhat:
Code:
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
Becomes:
Code:
#define e 3
#define g 1
#define h 2
#define f 0
#define j 8
#define k 6
From the looks of it, yes, the ' is a typo made at a later time, because what's happening is a bit shift which is split in half by it.
Quzah.
-
That part I understand, I don't have a problem understanding the macros, as long as I use a pen and paper to follow what is happening. My main question is about main (no pun intended). As you say, I guess you were at one point able to call main, but what about the parameters passed, and why is char *s declared before the body? I guess that the function is set to return int by default, or else he shouldn't be able to return any values.
-
> main(m1,s) char *s;
This is K&R syntax for a function definition.
m1 being an int, is assumed an int and omitted
-
heh
i tried to fix it but i got a seg falut at
Code:
for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);