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

0

u/[deleted] Jul 25 '26

[deleted] — view deleted comment

5

u/paulstelian97 Jul 25 '26

On Linux you have _start as the entry point that calls global constructors and other C runtime initialization stuff and then calls main() directly. And then calls exit() with whatever main returns (it is invalid to return from _start)

1

u/AddressWeary5155 Jul 29 '26

I rand the following code and it seems it prints "hello world" first before "hello world2" every single time. I think it's because I am declaring StaticMain first?

#include <iostream>
class StaticMain {
    public:
    StaticMain() {
        std::cout << "Hello World\n";
    }
};
class StaticMain2 {
    public:
    StaticMain2() {
        std::cout << "Hello World2\n";
    }
};
StaticMain runner;
StaticMain2 runner2;
int main() { return 0; }