Wednesday 14 May 2014

Command Line Argument

Every C program must have a function called main(), which is the point where execution of the program starts. .When the program is executed, the command line argument are made available via three arguments to the function main(). So the function prototype for the main() function is
                    
              int main(int argc, char *argv[], char *envp[]);

The  Second parameter, *argv[] parameter is an array of  char pointers. The first element in  argv[]
always  points  to  the  program’s  name,  and  each  subsequent member points to a parameter( command-line arguments).
Some compilers and operating systems don’t provide the program name, but have argv[0] point instead to some predefined string constant, such as "C".   Each parameter is separated by the operating system’s default parameter separator, usually a blank or comma. Under the PC’s DOS operating system, only a blank is used as a separator. 

The First parameter, argc parameter, an integer, contains the number of elements in the passed
array of  argv[]. The list of pointers in argvis terminated by a NULL pointer (i.e., argv[argc] is NULL).

The Third argument, *envp[] parameter,is an array of  char pointers or is an array of strings representing the variables set in the user's environment. I give u only small intro in environment variable:

Each process has an associated array of strings called the environment list, or simply the environment. Each of these strings is a definition of the form name=value. Thus, the environment represents a set of name-value pairs that can be used to hold arbitrary information. The names in the list are referred to as environment variables.

A simple program that prints both the passed parameters and the environment strings to the screen. This program’s output depends somewhat on which operating system it runs


#include                // Make includes first part of file
#include              // For string functions.

int main( int argc, char *argv[], char *envp[] )
{
             int i;
             printf("\n");
             printf("Program name is '%s'\n\n",argv[0]);
                                   // argc includes the program name, so decrement for actual passed parameters.
             printf("Number of parameters %d \n\n",argc - 1);
                                  // It's just as valid is to use:
                                 // for (i = 1; i < argc; i++)
             for (i = 1; argv[i]; i++)
             {
                       printf("Passed parameter %2d is '%.50s'\n",i,argv[i]);
              }
              printf("\n");


                              // Environment variables may not be meaningful for all
                              // operating systems. Check the compiler's documentation.
                              // If this information is not available on your system,
                             // delete the below for() loop.
    

             for (i = 0; envp[i]; i++)

             {

                       printf("Environment string %2d is '%.50s'\n",i,envp[i]);

              }

              getchar();

              return (0);

}
Output of this program in my system using cygwin is like this:


0 comments :

Post a Comment