john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

commandline parameters pass arguments

/* john pfeiffer 19sep07 Using command line arguments/parameters in C */
#include <stdio.h>

#define CMDLINE_PARAMETERS 2        /* The first parameter is always the program name */

void program_usage_help( char* argv[] )
{   printf("%s filename.htm",argv[0]); 
}/* end program_usage_help */

void test_commandline_parameters( int argc, char* argv[] )
{   int i = 0;
    if( argc < CMDLINE_PARAMETERS )
    {   printf("\nMore parameters needed, e.g.\n"); 
        program_usage_help( argv );
        exit(1);
    }
    else if( argc > CMDLINE_PARAMETERS )
    {   printf("\nToo many command line parameters, e.g.\n");   
        program_usage_help( argv );
        exit(1);
    }
    else
    {   while( i < argc )
        {   printf("%s ", argv[i]);     
            i++;
        }
        printf("in progress...\n\n");
    }

}/* end test_commandline_paramters func */
/*  char buffer[256];   //possible concatenating alternative to below
    int i;
    for(i=0; i<argc; i++)
    {   strcat(buffer,argv[i]);
        strcat(buffer, " ");
    }    
    printf("%s",buffer);
*/

int main(int argc, char* argv[])
{
    int i=0;
    printf("Program: ");
    while( i < argc )
    {   
        printf("%s ",argv[i++]);
    }
    printf("is still in progress...\n");

    test_commandline_parameters( argc, argv );

    return 0;
}/* end main */

  • « passing pointers to functions
  • file pointers in C »

Published

Sep 19, 2007

Category

c

~117 words

Tags

  • arguments 4
  • c 95
  • commandline 12
  • parameters 15
  • pass 3