Using command-line arguments in C++

By: Lakshmi Emailed: 1697 times Printed: 2193 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Many operating systems, such as DOS and UNIX, enable the user to pass parameters to your program when the program starts. These are called command-line options, and are typically separated by spaces on the command line. For example:

SomeProgram Param1 Param2 Param3

These parameters are not passed to main() directly. Instead, every program's main() function is passed two parameters. The first is an integer count of the number of arguments on the command line. The program name itself is counted, so every program has at least one parameter. The example command line shown previously has four. (The name SomeProgram plus the three parameters make a total of four command-line arguments.)

The second parameter passed to main() is an array of pointers to character strings. Because an array name is a constant pointer to the first element of the array, you can declare this argument to be a pointer to a pointer to char, a pointer to an array of char, or an array of arrays of char.

Typically, the first argument is called argc (argument count), but you may call it anything you like. The second argument is often called argv (argument vector), but again this is just a convention.

It is common to test argc to ensure you've received the expected number of arguments, and to use argv to access the strings themselves. Note that argv[0] is the name of the program, and argv[1] is the first parameter to the program, represented as a string. If your program takes two numbers as arguments, you will need to translate these numbers to strings. On Day 21 you will see how to use the standard library conversions. Listing below illustrates how to use the command-line arguments.

Using command-line arguments.

1:     #include <iostream.h>
2:     int main(int argc, char **argv)
3:     {
4:        cout << "Received " << argc << " arguments...\n";
5:        for (int i=0; i<argc; i++)
6:           cout << "argument " << i << ": " << argv[i] << endl;
7:     return 0;
8: }

Output: TestProgram  Teach Yourself C++ In 21 Days
Received 7 arguments...
argumnet 0: TestProgram.exe
argument 1: Teach
argument 2: Yourself
argument 3: C++
argument 4: In
argument 5: 21
argument 6: Days

Analysis: The function main() declares two arguments: argc is an integer that contains the count of command-line arguments, and argv is a pointer to the array of strings. Each string in the array pointed to by argv is a command-line argument. Note that argv could just as easily have been declared as char *argv[] or char argv[][]. It is a matter of programming style how you declare argv; even though this program declared it as a pointer to a pointer, array offsets were still used to access the individual strings.

On line 4, argc is used to print the number of command-line arguments: seven in all, counting the program name itself.

On lines 5 and 6, each of the command-line arguments is printed, passing the null-terminated strings to cout by indexing into the array of strings.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(10)


1. View Comment

Simple and to the point. Thanks for this tutorial.

View Tutorial          By: Balvinder at 2008-08-03 04:40:05
2. View Comment

Very useful. Thank you. Would be interesting to know how the program obtains argc? And what type of storage class is it? Is it possible to reuse the variable?

View Tutorial          By: Sergei at 2009-02-07 19:18:46
3. View Comment

what does argc means?

View Tutorial          By: Don Panganiban at 2009-10-09 21:19:48
4. View Comment

A good and useful tutorial for beginners.
thanks a lot.


View Tutorial          By: Hayk at 2010-01-12 10:55:15
5. View Comment

@3
argc simply means the argument count.

Thanks for the explanation on command line arguments. It provided me with a better understanding of its purpose.


View Tutorial          By: CDub at 2010-02-22 09:32:54
6. View Comment

can u give a gud example on command line arguments

View Tutorial          By: shravan at 2010-04-09 23:11:18
7. View Comment

This came out of a book

View Tutorial          By: Kyle Bradley at 2010-04-12 19:48:12
8. View Comment

well,its better to teach with video how it works

View Tutorial          By: c.muthukumar at 2010-09-22 07:28:24
9. View Comment

It is nice...

View Tutorial          By: Bunty at 2011-02-20 23:43:53
10. View Comment

in tc not accept argument directly run and gave file name in argument 1 so plz tc correct program gave

View Tutorial          By: vishal vekariya at 2012-11-16 11:06:38

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Lakshmi
Exporting a Certificate Authenticating Your Public Key example using keytool in Java
while and do-while example in Java
Common SQL Commands
Data Access Technologies in Java
perror() Function - example program in C
Struts-GUI and Struts Console
Variable-length Argument Lists sample program in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Use ImageButtonBean for Image based Form Submits in Struts
break and continue loops in C
Arguments - Call by Value in C
Using command-line arguments in C++
How to handle Exceptions in C++
Gauge sample program in J2ME
What is Reflection? - getConstructors(), getFields(), and getMethods()

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Dot (.) vs Arrow (->) to access data members in C++
Using Comments in a C++ Program
assert() example program in C++
Using cout.fill() in C++
Reverse a String in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Demonstration of Prefix and Postfix operators in C++
Constructors and Destructors in C++
while Loops in C++
while (1) Loops in C++
Using switch Statements in C++
The indirection operator (*) - dereference operator.
Stray or Dangling Pointers in C++
C ++ Reference Parameters Versus Java Reference Parameters
Converting C++ Multiple-Inheritance Hierarchies to Java
Most Emailed Articles (in C++)
Using cout in C++
Demonstration of Prefix and Postfix operators in C++
qsort() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Escape Sequences for Nonprintable Characters in C++
A Brief History of C++
C++ and Object-Oriented Programming
Use of Conditional (Ternary) Operator in C++
Returning values from a function in C++
Constructors and Destructors in C++
Interface Versus Implementation in C++
The Stack and the Free Store in C++
this Pointer in C++
How to handle Exceptions in C++
Using cout.width() in C++