In c the main()
function can take two parameters the first one being the number of arguments and the second an array with the arguments, so you wont need to store each string anywhere you can use this array for the lifetime of the program.
Here is an example of you to print these arguements to the standard output
int main(int argc, char **argv)
{
int index;
for (index = 1 ; index < argc ; ++index)
printf("Argument %d -> %s\n", index, argv[index]);
return 0;
}
as you can see, the number of arguments is argc
, the first argument if the program was invoked from the shell will be the program name, the next arguments are the arguments passed to the program.
Suppose you complied the program like this
gcc -Wall -Wextra -Werror -O0 -g3 source.c -o program
then you can invoke the program this way
./program FileName1 "A file name with spaces embeded" another_file
the output would be
Argument 1 -> FileName1
Argument 2 -> A file name with spaces embeded
Argument 3 -> another_file
usually argv[0]
is the program name, but you can't guarantee that, if the program is invoked from a shell, it's pretty safe to assume that though.