Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - reading command line parameters

Tags:

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?

like image 640
Waypoint Avatar asked Mar 01 '11 16:03

Waypoint


People also ask

How do you pass command line arguments in C?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

What is C command line arguments?

What are Command Line Arguments in C? Command line arguments are nothing but simply arguments that are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution.


2 Answers

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

  • int main(void)
  • int main(int argc, char **argv)

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:

  • int argc - the number of arguments passed into your program when it was run. It is at least 1.
  • char **argv - this is a pointer-to-char *. It can alternatively be this: char *argv[], which means 'array of char *'. This is an array of C-style-string pointers.

Basic Example

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

#include <stdio.h>  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 

Now run it...

[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...

./myprogram integral


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 <stdio.h> #include <string.h>  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.

like image 116
逆さま Avatar answered Sep 25 '22 20:09

逆さま


#include <stdio.h> #include <stdlib.h>  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; } 
like image 44
pmg Avatar answered Sep 24 '22 20:09

pmg