Command Line Arguments In C

Example

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

Here, argc is responsible for tallying the total count of arguments passed to the program, which includes considering the file name as the initial argument.

The argv array stores the overall count of arguments passed. The initial argument consistently represents the file name.

Example

Let's examine an instance of command line parameters where a single argument is being passed along with the file name.

Example

Example

#include <stdio.h>

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



   printf("Program name is: %s\n", argv[0]);

 

   if(argc < 2){

      printf("No argument passed through command line.\n");

   }

   else{

      printf("First argument is: %s\n", argv[1]);

   }

}

Run this program as follows in Linux:

Example

./program hello

Execute this program in the following manner on Windows using the command line:

Example

program.exe hello

Output:

Output

Program name is: program

First argument is: hello

If multiple arguments are passed, only a single one will be printed.

Example

./program hello c how r u

Output:

Output

Program name is: program

First argument is: hello

However, when multiple arguments are enclosed in double quotes, they will be considered as a single argument.

Example

./program "hello c how r u"

Output:

Output

Program name is: program

First argument is: hello c how r u

You have the option to develop your program to display all the arguments provided. The current implementation only prints argv[1], resulting in the display of just one argument.

Input Required

This code uses input(). Please provide values below: