The arguments of the main( ) function
 

The main( ) function in C has 2 arguments (= parameters):

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      printf( "Hello World !\n" );
   } 
   

argc = argument count is the # parameters given in the command line

argv = argument vector is the array variable containing the individual argument strings

 

Compared to Java:    argc ~= Java's args.length        argv[i] ~= Java's args[i]

Command line and arguments in UNIX
 

  • When you type a command in a UNIX terminal:

         %    command     arg1    arg2
                ^          ^       ^
                |          |	     |
              argv[0]   argv[1]  argv[2]
      
        There are 3 arguments in the command line
      

    (% is the prompt)

Sample C program that shows the arguments
 

The following for-loop prints the individual argument vector:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      for ( int i = 0; i < argc; i++ )
         printf("%s\n", argv[i]); // prints argv[i] out 
   } 
   

DEMO:   /home/cs255001/demo/C/set1/args.c