23 comments

[ 4.6 ms ] story [ 57.9 ms ] thread
Like most code, this is way too easy in Perl 6.

    > perl6 -e 'say "Life, the Universe and Everything".WHY'
    42
Beautiful code is simple maintainable code. In C this is a simple as:

#include <stdio.h>

int main(void) { puts("42"); return 0; }

Beautiful code denies its own existence so perfectly you forgot you wrote it
I was curious why you would return a value and not just make the main function return void (in the interests of simplicity and maintainability, and assuming you're using C not C++). So I wrote my own such program, and discovered that void main(void) returns 3 to the OS. Any ideas why?
Because returning void means that, on most platform, return whatever is left in %eax at the time of function termination.
I submitted it already in the linked comments section, but here it is again:

    #include <iostream>

    int main(void)
    {
        std::cout << (6 * '9') % ('h'*'2'-'g'*'2') << std::endl;
    }
ATDT42

(Coz 42 is Gods phone #, duh...)

MAJIKTHISE: I mean what’s the use of us sitting up all night saying there may -

VROOMFONDEL: Or may not be

MAJIKTHISE: …or may not be… a god, if this machine comes along the next morning and gives you ‘is telephone number?

    #include <stdio.h>

    #define SIX 1+5
    #define NINE 8+1

    int main(void)
    {
        printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
        return 0;
    }
https://github.com/Keith-S-Thompson/42
I never thought code could make me feel like this.
Ever waste days debugging a subtle bug introduced by C macros? It's PTSD material.

I love C but I will never understand why C has a naive macro system.

I always thought it was a historical choice that simple substitution languages were simpler to implement so were chosen as a default.

It is kind of infuriating that the preprocessor always feels simultaneously _too powerful_ and _not powerful enough_ for the jobs you want to get done. I find myself reaching for other tools (kaitai, m4 etc) a lot more these days to take the role.

The kenc[1] compiler subsequent to K&R C had much more constrained macro system exactly for those reasons. Thanks to advances in optimization and improved machine speed, it was reasonable to skip preprocessor-level const operations. Thanks to dead code elimination it was feasible to mostly give up conditional compilation.

[1] as used, for example, in Plan 9.

Could be "improved"

  #define SIX 1+8
  #define NINE 5+1