Linux C Programming tutorial part 22 - Accessing command line arguments within C program

In the previous tutorial, we discussed multiple concepts related to pointers in C programming language. One of the concepts we discussed was an array of pointers. We used the example of an integer pointer array to demonstrate the concept.

How to access command line arguments within a C program

But as mentioned in that tutorial, an array of pointers can be of different types. For example, here's an array of character pointers:

char *arr[] = {"HowtoForge", "FaqForge", "Linux"};

And here's a small program that shows how this array can be used:

#include <stdio.h>

int main()
{
char *arr[] = {"HowtoForge", "FaqForge", "Linux"};

printf("\n %s", *arr);
printf("\n %s", *(arr+1));
printf("\n %s", *(arr+2));


return 0;
}

'arr' is the address of the first element of this array. Now, since this is an array of pointers, so the first element (and all other elements, accessed by adding 1 and 2 to arr) is nothing but address. So to access the value kept that address, we use the * operator.

Following is the output of this program:

HowtoForge 
FaqForge
Linux

So this is how you access values from an array of character pointers. Now, you must be thinking why we discussed all this?

Well, we did it because this brings us to another important concept, which is the ability to access command line arguments in environments that support the C programming language. Let's discuss this in detail.

As should be well aware by now, 'main' is the first function in your program that gets called when the program is executed. It's called with two arguments: argc and argv. While the former tells you about the number of command line arguments, the latter is actually an array of pointers to arguments themselves.

Please note that the name of the program itself is passed as an argument to the main function. So because of this, the number of arguments - represented by argc - is one more than the actual number of arguments passed to the program. Following is an example:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf("\n %d", argc);
printf("\n %s", *argv);

return 0;
}

Now, we executed this program - called a.out, and present in the 'home' directory of our system - without any arguments. And here's the output:

 1 
/home/a.out

So you can see the execution name of the program was passed as an argument. Had I used the following command:

/home/a.out htf ff

Then in that case, argc would have been 3 and argv would have contained addresses of strings '/home/a.out', 'htf', and 'ff'.

Here's a better way to write the arguments printing program:

#include <stdio.h>

int main(int argc, char *argv[])
{
for(int i=0; i<argc; i++)
printf("\n %s", *(argv+i));

return 0;
}

It's worth remembering that the arguments are stored in the array in the same order they are passed on the command line.

Conclusion

You will likely encounter several situations wherein you'll have to deal with command line arguments inside your C code. What we discussed here should form a basis for you. Do try out some programs at your end to get a better idea on how command line arguments are accessed inside a C program.

Share this page:

1 Comment(s)