Reading command line parameters

I have made little program for computing pi (π) as an integral. Now I am facing a question how to extend it to compute an integral, which will be given as an extra parameter when starting an application. How do I deal with such a parameter in a program?

359k 69 69 gold badges 545 545 silver badges 588 588 bronze badges asked Mar 1, 2011 at 16:31 17.7k 40 40 gold badges 121 121 silver badges 171 171 bronze badges So is your question really just about command line arguments? Commented Mar 1, 2011 at 16:33

yea, and then I don't know how to deal it with in actual program, if I read as a char than transfer it to int for computing? Just this is the part a dont understand

Commented Mar 1, 2011 at 16:35 Commented Mar 1, 2011 at 16:40 (Title edited to make more appropriate) Commented Mar 1, 2011 at 16:51

4 Answers 4

When you write your main function, you typically see one of two definitions:

The second form will allow you to access the command line arguments passed to the program, and the number of arguments specified (arguments are separated by spaces).

The arguments to main are:

Basic Example

For example, you could do this to print out the arguments passed to your C program:

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

I'm using GCC 4.5 to compile a file I called args.c . It'll compile and build a default a.out executable.

[birryree@lilun c_code]$ gcc -std=c99 args.c 
[birryree@lilun c_code]$ ./a.out hello there argv[0]: ./a.out argv[1]: hello argv[2]: there 

So you can see that in argv , argv[0] is the name of the program you ran (this is not standards-defined behavior, but is common. Your arguments start at argv[1] and beyond.

So basically, if you wanted a single parameter, you could say.

A Simple Case for You

And you could check if argv[1] was integral , maybe like strcmp("integral", argv[1]) == 0 .

So in your code.

#include #include int main(int argc, char **argv) < if (argc < 2) // no arguments were passed < // do something >if (strcmp("integral", argv[1]) == 0) < runIntegral(. ); //or something >else < // do something else. >> 

Better command line parsing

Of course, this was all very rudimentary, and as your program gets more complex, you'll likely want more advanced command line handling. For that, you could use a library like GNU getopt .

1 1 1 silver badge answered Mar 1, 2011 at 16:47 79.3k 16 16 gold badges 168 168 silver badges 177 177 bronze badges
#include #include int main(int argc, char **argv) < int i, parameter = 0; if (argc >= 2) < /* there is 1 parameter (or more) in the command line used */ /* argv[0] may point to the program name */ /* argv[1] points to the 1st parameter */ /* argv[argc] is NULL */ parameter = atoi(argv[1]); /* better to use strtol */ if (parameter >0) < for (i = 0; i < parameter; i++) printf("%d ", i); >else < fprintf(stderr, "Please use a positive integer.\n"); >> return 0; > 
answered Mar 1, 2011 at 16:53 109k 14 14 gold badges 128 128 silver badges 201 201 bronze badges

Parsing command line arguments in a primitive way as explained in the above answers is reasonable as long as the number of parameters that you need to deal with is not too much.

I strongly suggest you to use an industrial strength library for handling the command line arguments.

This will make your code more professional.