Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does argc come from?

A new process is created by fork/exec. Exec sets up the command line arguments but I don't see that it sets up the number of those arguments to put in argc.

main() is supposed to be the first function to run in the new process but argc is already set by then.

Where is it set? It has to be some kind of setup code that counts parameters before main is called but nothing I have read explains what kinds of things this setup code does or where it resides.

Is this in libc? Is this the same on every OS and covered by some specification? Where would I find what goes on in the setup code besides this? Is it called before instantiating globals?

like image 554
Translucent Pain Avatar asked Mar 15 '26 11:03

Translucent Pain


1 Answers

There's plenty of stuff that happens in most implementations before main is called (usually environment set up by something similar to crt0, the C runtime startup).

There's also plenty of stuff that may happen after main exits, such as resource closure, atexit exit handlers and so forth.

The C standards only really cover the stuff that happens in terms of the language, not how implementations do their work under the covers (which is basically what you're asking). Each implementation may do things in different ways but many UNIX types will have something like the afore-mentiond crt0 which does the setup.

like image 188
paxdiablo Avatar answered Mar 18 '26 01:03

paxdiablo