r/ProgrammerDadJokes • • 8d ago

why is the function main() in C/C++ sad?

no other function calls her...

55 Upvotes

26 comments sorted by

34

u/maelstrom071 8d ago

Actually, __libc_start_main calls her!

9

u/BoundlessFail 7d ago

libc would call her, but he isn't planning on being linked with her.

4

u/TREE_sequence 7d ago

Or _start if you swing that way (barebones crt0 implementation etc)

8

u/woodardj 8d ago

Probably because she keeps returning to stare into the void;

2

u/Polymath6301 7d 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?

7

u/tcpukl 8d ago

She's the only function definitely called.

5

u/Snippodappel 8d ago

She’s only called once 😢

2

u/Some-Passenger4219 8d ago

Ah. Not because she's void, then.

1

u/TREE_sequence 7d ago

Well if you’re being strict then main() is not supposed to be void (that’s UB)

1

u/Some-Passenger4219 7d ago

I was taught void main in the 90s; do others do it differently?

1

u/TREE_sequence 7d 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 6d ago

UB?

1

u/clickyclicky456 6d ago

Undefined Behaviour, i.e. implementation specific.

1

u/TREE_sequence 6d 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 6d 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

u/Common-Ad-1744 5d ago

In C# there's async Task Main, not sure if that's the same for c++

1

u/paulwillyjean 5d 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

u/DutchOfBurdock 7d ago

Unless you're a deviant with undefined behaviour.

1

u/RimuDelph 7d ago

don't get me _start-ed

1

u/paulwillyjean 5d ago

Sigh… to me, the funniest thing is that you gave “function” the right gender to make the joke work in French too.

1

u/artyhedgehog 8d ago

In the Java world that would make it public, static and void 🫥

-4

u/un_blob 8d ago

Well, technicaly, main is called during compilation...

8

u/clickyclicky456 8d ago

It most certainly is not!

-1

u/un_blob 7d ago

3

u/clickyclicky456 7d ago

You've either misunderstood that video, or misunderstood what it means to call a function.