Table of contents
1.
Introduction
2.
What are Command Line Arguments?
2.1.
Example
3.
Properties of Command-Line Arguments
3.1.
Output in different scenarios
4.
Following are the Command Line arguments passed
5.
How To Read/Get Command Line Arguments?
6.
Frequently Asked Questions
6.1.
What are command-line arguments in C++?
6.2.
How to run command-line arguments in C in Dev C++?
6.3.
How do I create a command line argument?
6.4.
What is the use of command line? 
7.
Conclusion
Last Updated: Aug 6, 2024
Easy

Command Line Arguments in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article will give you in-depth information about the command line arguments 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. 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.

Fibonacci Series in C++

What are Command Line Arguments?

Command Line Arguments are values or parameters passed to a program through the command line or terminal when it is executed. They allow users to customize program behaviour or pass data to a program without modifying the code. Command Line Arguments can be used in various programming languages and are commonly used in command-line utilities and scripts.

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

Try and compile with online c++ compiler.

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.

Also see, fibonacci series in c

How To Read/Get Command Line Arguments?

In C++, the main function can take two arguments: argc and argv. Which represent the number of command line arguments and an array of character pointers representing the actual arguments, respectively.

To access individual arguments, you can simply index into the argv array, starting from 0. For example, argv[0] will contain the name of the program being executed. argv[1] will contain the first command line argument.

You can use a for loop to iterate through all the command line arguments, with argc as the loop counter.

Also see, gzip command in linux

Frequently Asked Questions

What are command-line arguments in C++?

Command-line arguments in C++ are values or parameters that are passed to a C++ program via the command line or terminal. They are represented as an array of character pointers, with the first element being the name of the program.

How to run command-line arguments in C in Dev C++?

To run command-line arguments, first, write a C program that accepts command-line arguments. Then, in Dev-C++, go to the "Project" menu, select "Project Options", click on the "Parameters" tab, and enter the command-line arguments in the "Parameters" box. Finally, run the program.

How do I create a command line argument?

To create a command line argument, simply pass a value or parameter after the program name when executing a program in the command line or terminal. The program can then retrieve and use these values as arguments during its execution.

What is the use of command line? 

A command line is a text-based interface used to enter commands and interact with a computer system. It's a powerful and efficient way to run programs, execute tasks and automate repetitive tasks, and it is available on various operating systems.

 

Also see - Difference between argument and parameter

Conclusion

In this article, we discussed Command Line Arguments in C++. We also saw the approach and Java code to solve it. Hope this article helped you to understand this question in an easy way and motivated you through your coding journey.

Check out other related articles to learn more: 

And many more on our platform Coding Ninjas Studio

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses. Refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass