Showing posts with label Command Line Argument. Show all posts
Showing posts with label Command Line Argument. Show all posts

Saturday 31 March 2012

Command Line Argument


Command Line Argument

Command line argument is the parameter which supplied to program when the program is invoked.

Every C program  must start execution from the main function and it is starting of program. Same as other function main() also take arguments as  their value. The main() function can receive two arguments called argc and argv. Consider  following format of main()

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

Here, The first argument argc is an argument counter that count  the total number of command line arguments including the name of program.

The second argument argv[] is an argument vector which is the full list of all command line arguments.It represents an array of character pointer  that point to the command line arguments. The size of this array is equal to the value of  argc.

Ex,

#include<stdio.h>
#include<conio.h>

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

    printf("\n Total argument : %d",argc);


    for(i=0;i<argc;i++)
      printf("\n argv[%d] = %s",iargv[i]);
    getch();
 }

è                              To execute this program first save this program as comm.c , Compile this file(alt+f9) and then ctrl+f9. It means that first need to create its .exe file.
è                              Select File menu -> Dos Shell.
è                               After that pass the arguments with file name as shown below:
C:\tc\bin> comm this is c program
 The output will be:
argv[0]= c:\tc\bin\comm.c
argv[1]= this
argv[2]= is
argv[3]= c
argv[4]=program

The first parameter in the command line is always the program name.

Posted By : Ruchita Pandya