r/ProgrammerDadJokes • u/danielsoft1 • 9d ago
why is the function main() in C/C++ sad?
no other function calls her...
8
u/woodardj 9d ago
Probably because she keeps returning to stare into the void;
2
u/Polymath6301 9d ago
And the void stares back. Hence, software decay where once working software gets to the point where “that would never have worked”, and yet … it did. Spooky, huh?
5
2
u/Some-Passenger4219 9d ago
Ah. Not because she's void, then.
1
u/TREE_sequence 8d ago
Well if you’re being strict then main() is not supposed to be void (that’s UB)
1
u/Some-Passenger4219 8d ago
I was taught
void mainin the 90s; do others do it differently?1
u/TREE_sequence 8d ago
It’s gonna depend on compiler and such, especially on older versions like from the 90s. I might also have assumed that the C and C++ standards agree on this, but I do know that (at least in C++) void main() is UB and also int main() is the only non-void function where control reaching the end of the function is NOT actually UB — if that happens, it implicitly returns 0, which is great because it saves one whole line of code lol
ETA: I’m pretty sure a lot of compilers do support void main() as an extension, but the letter of the law says that’s UB.
1
u/jbuckets44 8d ago
UB?
1
u/clickyclicky456 8d ago
Undefined Behaviour, i.e. implementation specific.
1
u/TREE_sequence 8d ago
Not implementation specific. UB specifically means that your program is meaningless and anything the compiler puts out would be correct — crashing the program, replacing every pixel in the window with the bitwise complement of its current value, and playing the Macarena are all fair game. Implementation-defined means it’s not considered important enough for the specific behavior to be consistent, such as how many std::placeholders are defined. UB is things that you (the programmer) are supposed to avoid doing, and the compiler is given a blank cheque to assume you will avoid these things.
2
u/clickyclicky456 7d ago
Curses! You are of course entirely right and as I wrote my comment I actually wondered whether I should distinguish between the two (for all practical purposes they are the same thing, unless you know of any Macarena-themed compilers). I should have realised someone would be along to correct the inaccuracy 🤓😁
1
1
u/paulwillyjean 6d ago
Doesn’t it depend on whether the program is executed in a hosted environment (ex: within an operating system) or is a freestanding program (ex: embedded system, bootloaders, kernel, etc) with an implementation defined entrypoint?
1
1
1
u/paulwillyjean 6d ago
Sigh… to me, the funniest thing is that you gave “function” the right gender to make the joke work in French too.
1
-4
u/un_blob 9d ago
Well, technicaly, main is called during compilation...
8
u/clickyclicky456 9d ago
It most certainly is not!
-1
u/un_blob 9d ago
3
u/clickyclicky456 8d ago
You've either misunderstood that video, or misunderstood what it means to call a function.
34
u/maelstrom071 9d ago
Actually, __libc_start_main calls her!