Live data from Hacker News

The Most Beautiful Piece of Code That Prints 42

fluentcpp.com

11–20 of 24 posts

Re: The Most Beautiful Piece of Code That Prints 42

#14
post #7

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

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.

Re: The Most Beautiful Piece of Code That Prints 42

#15

Beautiful code is simple maintainable code. In C this is a simple as: #include int main(void) { puts("42"); return 0; }

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?

Re: The Most Beautiful Piece of Code That Prints 42

#17

Beautiful code is simple maintainable code. In C this is a simple as: #include int main(void) { puts("42"); return 0; }

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.

Re: The Most Beautiful Piece of Code That Prints 42

#19
post #7

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

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.

Post reply on HN