Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

you can have pre-main functions in in C as well (as a GCC extension). If you define functions with `__attribute__(constructor)` and they will be called before main(). You can even set the priority on them, and set post-main destructor callbacks. This works even for shared libraries loaded in run-time.

I've used this as a trick to automatically run unit tests, though, not for any real work.



"Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should."


It can be useful for registering things automatically, like Go's init() functions which I've used on occasion.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: