C Command Line Arguments

Command line arguments are passed inside the main function of a C program also called as command line of the C program, hence the name, command line arguments.

Syntax:

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

  • Argc: This is a mandatory parameter that counts the number of arguments.
  • Argv[]: This is also a mandatory parameter that contains all the arguments.

Example:

#include <stdio.h>  
void main(int argc, char *argv[] )  
{  
printf("File name: %s\n", argv[0]);  
 
if(argc < 2)
{  
      printf("No argument passed.\n");  
   }  
else
{  
      printf("Argument 1: %s\n", argv[1]);  
   }  
}

Output

File name: C_program
Argument 1: welcome