Thread: Complicated Hello World

  1. #1
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594

    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

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    seems pretty natural. i mean, thats how i would go about writting hello world

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    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

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > main(m1,s) char *s;
    This is K&R syntax for a function definition.
    m1 being an int, is assumed an int and omitted
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM