Programming in "C‟ | Describe the concept of command line argument with example.

Command line arguments are given after the name of the program in command-line shell of Operating System. The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program.

Example: 

#include <stdio.h>

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

{

 printf(" \n Name of my Program %s \t", argv[0]); 

 if( argc == 2 )

    {

 printf("\n Value given by user is: %s \t", argv[1]);

    }

    else if( argc > 2 )

    {

 printf("\n Many values given by users.\n");

    }

    else

    {

 printf(" \n Single value expected.\n");

    }


Post a Comment

0 Comments