Hacker News new | past | comments | ask | show | jobs | submit login
The Most Beautiful Piece of Code That Prints 42 (fluentcpp.com)
73 points by DmitryNovikov on May 13, 2018 | hide | past | favorite | 23 comments



    #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.


Nice.


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


Like most code, this is way too easy in Perl 6.

    > perl6 -e 'say "Life, the Universe and Everything".WHY'
    42


For non perl users - does this really work or are you being ironic?



Today I learned about meta methods. Thanks :)


I searched for a Perl6 repl to try it on and it works! Perl is so whimsically fun. :)

https://tio.run/#perl6


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;
    }


Freek Wiedijk's Most Functional winner of the 2014 International Obfuscated C Code Contest is an obvious candidate.:

[1] https://www.ioccc.org/2014/wiedijk/prog.c

[2] https://www.ioccc.org/2014/wiedijk/hint.html


It's too bad the code examples have to be in C++. When I saw the prompt, it brought to mind the fabulous Shakespeare Programming Language, which would allow for a more poetic approach:

http://shakespearelang.sourceforge.net/report/shakespeare/sh...



HTML version:

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.


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?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: