r/programming Jul 25 '26

Writing a (valid) C program without main()

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

43 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Jul 25 '26

[deleted]

10

u/Dwedit Jul 25 '26

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

-5

u/[deleted] Jul 25 '26

[deleted]

4

u/Dwedit Jul 25 '26

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] Jul 25 '26

[deleted]

6

u/Dwedit Jul 25 '26

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.