User interacts with shell by giving commands and some arguments to those commands.
These commands are generally some C program located in /bin,/usr/bin,/usr/local/bin folder.
We generally do not give full path of the command.
So the shell of the OS maintains an environment variable called PATH to search for a binary executable when a full path is not given.
Shell is a process that collects these arguments and creates a new task with this given command string/binary name. It also passes user arguments and environment variables. fork is used to spawn a new task and a series of execxxcalls are there to overwrite process with new binary and to pass new arguments and environments.
We all know C program starts with a main() routine.
Here we can access these arguments via argv also known as argument vector.
Argument vector is a pointer to a pointer which contains a list of strings.
This list or array contains a series of pointers starting from 0 which is program name/path the arguments placed at one after another. Argc or argument count is the first argument which holds the total number of elements in this argv. This is all about user arguments. Now C runtime also maintains a similar vector for environment strings. This vector is named as environ. This vector contains a name value pair separated by ‘=”. One point to note here is this vector contains a NULL entry at the end. This is to indicate that it is the last entry.
We have two demo application code to exchange this thoughts.
Lastly one question: Is main() the actual starting entry point of C program?
The answer is no. Actually main() is not the entry point of the process.
A process generally starts with a symbol _start().
Every C program has a hidden startup routine.
This is mainly written with assembly and C and a part of C compiler/linker.
Compiler binds this small code with the output binary as a a part of static linking process.
This part of the code sets segment registers and several init stuffs including construct stack
and user mode portion of memory allocator. It also constructs this argument vector and environment vector.