I'm currently learning C by working through K&R's The C Programming Language, and have reached the point in the book where command-line arguments are discussed. In the book, the main routine would be written something like this:
int main(int argc, char *argv[])
{
do something
}
From my understanding, at some point the number of arguments passed to the program must be counted and stored in argc
. Also, the arguments themselves must be stored, and pointers to the first characters of each are stored in the array argv
, where argv[0]
is a pointer to the name of the command and argv[argc]
is a null pointer. These steps can't just magically occur, this behaviour must be defined somewhere!
As an example, imagine that I want to store the first character of each argument passed to the program, firstc
, and discard the remainder of that argument (let's pretend that I had a really, really good reason for doing this). I could write main() like so:
int main(char firstc[])
{
do something
}
Clearly, this can already be done quite easily with the default argc
and argv
, and I wouldn't actually do it in practice. I can't even imagine a scenario in which this would actually be necessary, but I'm curious to know if it's possible.
So my (entirely theoretical, completely impractical) question is this: is it possible to define my own behaviour for the command line arguments? If it is, how would one go about doing so? If it's relevant, I'm using Ubuntu 16.04 and the GNOME Terminal.
P.S.
I just realized while writing this question that it is entirely possible (perhaps probable) that the C script is completely blind to what's going on outside, and that the terminal emulator is what prepares the command-line arguments for the C program.