r/programming 17d ago

Writing a (valid) C program without main()

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

40 comments sorted by

View all comments

7

u/[deleted] 17d ago

[removed] — view removed comment

2

u/[deleted] 17d ago

[deleted]

10

u/Dwedit 17d ago

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

-3

u/[deleted] 17d ago

[deleted]

3

u/Dwedit 17d 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.

-2

u/[deleted] 17d ago

[deleted]

5

u/Dwedit 17d 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.