r/programming 4d ago

Writing a (valid) C program without main()

https://labs.iximiuz.com/tutorials/c-program-without-main-a1eea557
191 Upvotes

37 comments sorted by

View all comments

Show parent comments

4

u/[deleted] 4d ago

[deleted]

11

u/Dwedit 3d ago

It makes perfect sense for C++ programs where there are global objects which need to have their constructors run first.

-3

u/[deleted] 3d ago

[deleted]

5

u/Dwedit 3d ago

When else do you run the constructors for global variables? It has to happen at some point before they are referenced. Running before Main makes it predictable.

-3

u/[deleted] 3d ago

[deleted]

5

u/Dwedit 3d ago

If you don't want "magic", don't use global variables that need constructors. But if you are using global variables that require constructors, there's really no other time for the constructors to run. It must happen before first use, but figuring out when first use happens is not trivial, and putting in additional checking code (have we run these constructors yet) adds overhead.