Professional Documents
Culture Documents
Command Line Arguments
Command Line Arguments
Introduction
• A command-line argument is the information that
follows the name of the program on the
command line of the operating system
1
Introduction
• C++ defines two built-in parameters to main()
– They receive the command line arguments
– Their names are argc and argv
• The names of the parameters are arbitrary. However, argc
and argv have been used by convention for several years.
– They are optional
• argc is an integer
– Holds the number of arguments on the command line
– Since the name of the program always corresponds to
the first argument, it is always at least 1
2
int main( int argc, char *argv[] )
#include <iostream>
using namespace std;
return 0;
}
3
Passing numeric command-line arguments
#include <iostream>
#include <cstdlib>
using namespace std;
4
[cgunduz@knuth cgunduz]$ g++ prog2.cpp -o exe_2
[cgunduz@knuth cgunduz]$ ./exe_2
Usage:
1. Integer (0) or double (1) division
2. Operand 1
3. Operand 2
[cgunduz@knuth cgunduz]$ ./exe_2 0 5 3
5\3 = 1
[cgunduz@knuth cgunduz]$ ./exe_2 1 5 3
5\3 = 1.66667
[cgunduz@knuth cgunduz]$ ./exe_2 0 8.2 2.9
8\2 = 4
[cgunduz@knuth cgunduz]$ ./exe_2 1 8.2 2.9
8.2\2.9 = 2.82759