Command Line arguments in C++

Command Line arguments in C++
Command Line arguments in C++

This article will give you in-depth information about the command line in C++ with uses and implementation.

The name is given after the name of the program in the command-line shell of Operating Systems. Whenever we write a program and run it into, we want some values to be input from the command line itself. These input values are called command-line values. It is handled using the main function.

Example 
int main( int argc, char *argv[] )
argc - Number of arguments passed 
argv[] - A pointer array which points to each argument passed in the program

Example:

Let us run this code on our Linux machine.

 
// Name of program commandline.cpp
#include <iostream>
using namespace std;
  
int main( int argc, char* argv[] )
{
    cout << "You have entered " << argc
         << " arguments:" << "\n";
  
    for (int i = 0; i < argc; ++i)
        cout << argv[i] << "\n";
  
    return 0;
}

Input:
$ g++ commandline.cpp -o main
$ ./main coding is fun

Output:
You have entered 4 arguments:
./main
coding
is
Fun

Properties of Command-Line Arguments:

  • They are passed to the main() function.
  • They are parameters/arguments supplied to the program when it is invoked.
  • They are used to control program from outside instead of hard coding those values inside the code.
  • argv[argc] is a NULL pointer.
  • argv[0] holds the name of the program.
  • argv[1] points to the first command-line argument and argv[n] point the last argument.

Note: You pass all the arguments separated by a space, but if the argument itself has space then you can pass such arguments by putting them inside double quotes “or single quotes.”

Example 
#include<stdio.h>
int main(int argc,char* argv[]) 
{ 
	int counter; 
	printf("Program Name Is: %s",argv[0]); 
	if(argc==1) 
		printf("\nNo Extra Command Line Argument Passed Other Than Program Name"); 
	if(argc>=2) 
	{ 
		printf("\nNumber Of Arguments Passed: %d",argc); 
		printf("\n----Following Are The Command Line Arguments Passed----"); 
		for(counter=0;counter<argc;counter++) 
			printf("\nargv[%d]: %s",counter,argv[counter]); 
	} 
	return 0; 
}

Output in different scenarios:

  • Without argument: When the above code is compiled and executed without passing any argument, it produces the following output.
    $ ./a.out

Program Name Is: ./a.out

No Extra Command Line Argument Passed Other Than Programme Name

  • Three arguments: When the above code is compiled and executed with three arguments, it produces the following output.
    $ ./a.out First Second Third

Program Name Is: ./a.out

Number Of Arguments Passed: 4

Following Are The Command Line Arguments Passed

argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third

  • Single Argument: When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output.
    $ ./a.out “First Second Third”

Programme Name Is: ./a.out

Number Of Arguments Passed: 2

Following are the Command Line arguments passed

argv[0]: ./a.out
argv[1]: First Second Third

  • The single argument in quotes separated by space: When the above code is compiled and executed with a single argument separated by space but inside single quotes, it produces the following output.
    $ ./a.out ‘First Second Third’

Program Name Is: ./a.out

Number Of Arguments Passed: 2

Following Are The Command Line Arguments Passed

argv[0]: ./a.out
argv[1]: First Second Third

getopt() Function

It is a function in C to pass command line arguments. 

Return Value: The getopt() function returns different values:

  • If the option takes a value, that value is a pointer to the external variable optarg.
  • ‘-1’ if there are no more options to process.
  • ‘?’ when there is an unrecognized option and it stores into external variable optopt.
  • If an option requires a value (such as -f in our example) and no value is given, getopt normally returns? By placing a colon as the first character of the options string, getopt returns: instead of? when no value is given.

Generally, the getopt() function is called from inside of a loop’s conditional statement. The loop terminates when the getopt() function returns -1. A switch statement is then executed with the value returned by getopt() function.

Syntax:

getopt(int argc, char *const argv[], const char *optstring)
optstring is simply  a list of characters, 
each representing a single character option.

Why don’t you check out Features & Data types in C++.

By Mansi Agarwal