Skip to content
Related Articles

Related Articles

C Program to Print the Program Name and All its Arguments

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 14 Mar, 2022

The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv [] is an array that contains values passed as arguments at the time of the run. The parameters passed through the command-line can be scanned in the program through command-line arguments.

Syntax:

int main(int argc, char *argv[])

Here,

  • argc is the integer type of argument that contains a total number of arguments passed through the command line.
  • argv[] is an array of character pointers, which contains all the parameters.

Example:

Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe
Output: args.exe

Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe 2
Output: args.exe 2

Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe akash bro
Output: args.exe akash bro

Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe geeks
Output: args.exe geeks

Below is the C++ program that accepts all the arguments from the user and prints every argument, including the file name. The program is compiled and executed successfully on Windows and Linux systems.

C++




// C++ program to implement
// command-lien arguments
#include <stdio.h>
  
// Command Line Arg 
void main(int argc, char *argv[])    
{
    int i;
    for (i = 0; i < argc; i++)
    {
        // Printing all the Arguments
        printf("%s ", argv[i]);        
    }
    printf("\n");
}

Output:

Output

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!