
The Most Beautiful Piece of Code That Prints 42 - DmitryNovikov
https://www.fluentcpp.com/2018/05/11/the-most-beautiful-piece-of-code-that-prints-42/
======
_kst_

        #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](https://github.com/Keith-S-
Thompson/42)

~~~
dynamicvariable
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.

~~~
anitil
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.

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

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

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

~~~
labster
[https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm...](https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L21)

Oh, it works.

~~~
sideshowb
Today I learned about meta methods. Thanks :)

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

------
tromp
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](https://www.ioccc.org/2014/wiedijk/prog.c)

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

------
zeta0134
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...](http://shakespearelang.sourceforge.net/report/shakespeare/shakespeare.html#SECTION00090000000000000000)

------
WiseWeasel
Cached:

[https://webcache.googleusercontent.com/search?q=cache:O9K6ou...](https://webcache.googleusercontent.com/search?q=cache:O9K6ouDFELQJ:https://www.fluentcpp.com/2018/05/11/the-
most-beautiful-piece-of-code-that-prints-42/)

------
RobertRoberts
HTML version:

42

------
aninteger
Beautiful code is simple maintainable code. In C this is a simple as:

#include <stdio.h>

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

~~~
askvictor
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?

~~~
innocenat
Because returning void means that, on most platform, return whatever is left
in %eax at the time of function termination.

------
primitur
ATDT42

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

~~~
cyberferret
_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?

