2/23/10

C example on how to use command line arguments

In this simple example we will see how to use the command line arguments in our C programs.
Well, we all noticed that main() get two parameters.
int main(int argc, char *argv[]);
argc is an integer representing the size of argv[]
argv is a table of pointer to chars ( Strings )
Below is a simple calculator which takes as arguments two numbers and prints the sum.
The code is:

#include <stdio.h>

int main(int argc, char *argv[]) {
if ( argc != 3) {
printf("Usage:\n %s Integer1 Integer2\n",argv[0]);
}
else {
printf("%s + %s = %d\n",argv[1],argv[2], atoi(argv[1])+atoi(argv[2]));
}
return 0;
}

Now lets explain the code:
line 4 : we check if the user passed two arguments to the program. We actually need two arguments but in C the first argument ( argv[0] ) is the name of our program, so we need two more.
line 5 : If the user didn't pass two arguments we print the usage of our program and exit
line 8 : Using atoi() function we convert pointers to char (string) to decimal numbers and display their sum

Example without arguments

C:\>ArgumentCalculator.exe
Usage:
ArgumentCalculator.exe Integer1 Integer2

C:\>

Example with two arguments

C:\>ArgumentCalculator.exe 123456789 987654322
123456789 + 987654322 = 1111111111

C:\>


In addition to the above the code to print all the arguments is:

#include <stdio.h>

int main(int argc, char *argv[]) {
for(int i = 0 ; i<argc ; i++)
printf("\nArgument %d: %s", i, argv[i]);
return 0;
}
Del.icio.us

12 comments:

謝郁卿 said...

IS VERY GOOD..............................

.net Code Protection said...

Nice Example. Things are becoming more and more simple with each day.

MLM Software India said...

Thanks for sharing . very informative information. I will bookmarking this post.

http://mlmdevelopers.com/products/mlm-software/mlm-software-beta/features.html

.net obfuscator said...

Nice post for the beginners.Keep up posting such informative post.

joomla web designs said...

It is another piece of your great work. Your site is very interesting and I read your posts each time something new appears. Thanks for your work.

Haziq said...

Thanks nice work. However, you could have described the program more, I think.

deepsharma said...

You always
Socialkik do something better and different.. keep it up

Mina Das said...

Great post. I like this c++ a lot. Thanks for this good information.
Computer for dummy

Robert Anderson said...

I have been desperately looking for info about this and discovered your website via yahoo. It is undoubtedly one for my bookmarking! Keep up  the good work I will pop back again soon.
Time and Attendance Software

William simth said...

Web Designing very informative information. I will bookmarking this post.

Recruitment Management System said...

Very nice explanation, Thanks for sharing.

floworks said...

wow... really nice code. I will try this code myself.. thanks for the free download

Related Posts Plugin for WordPress, Blogger...