Thread: Passing a parameter into the executable

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    Question Passing a parameter into the executable

    I want to create 12 shortcuts that point to the same executable. I want the command line of these shortcuts to pass two variables into the program. I know how to do this using batch programs (%1 %2) but have never done this with a C executable. Can anyone point me in the right direction?

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>

    int main( void ) {
    struct tm *tm_now;
    time_t now = time(NULL);
    int hour;
    int min;
    FILE *infile;

    tm_now = localtime(&now);

    tm_now->tm_min += 3; /* now add 3 minutes */
    now = mktime( tm_now ); /* normalise the time */

    tm_now = localtime(&now);

    hour = tm_now->tm_hour;
    min = tm_now->tm_min;

    infile = fopen("atline.bat", "w");
    fprintf(infile,"at \\\\TWPC-BELL-DC01 %d:%d c:\\securedox\\send\\fileprep 1 1\n", hour, min);
    fprintf(infile,"net send \\\\TWPC-BELL-WS02 The file will be sent at %d:%d", hour, min);
    fclose(infile);

    system("atline");

    return EXIT_SUCCESS;
    }

    This is my program, I'm going to want to receive two integers from the execution (1-12). I'd have "program 1 2". I want to take the "1" and store it as a variable and the "2" stored as another variable. Thanks in advance for any help!

    Sarah

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You can access the commandline parameters by declaring your
    main function like below. argc is then set to the number of
    commandline arguments you received, while the argv array
    is an array of strings, each one containing one argument.
    Just run this program, giving it as many parameters as you like:

    PHP Code:
    #include <stdio.h>

    int mainint argccharargv[] )
    {
        
    int i;

        for( 
    argc i++ )
        {
            
    printf"Commandline Parameter %d is: %s\n"iargv[i] );
        }
        
        return 
    0;

    Note that argv[0] is always the name of your executable.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You declare it like this:

    int main (int argc, char ** argv)

    then, your program's name is stored as argv[0].
    The first parameter is argv[1], the next argv[2], etc.

    argc tells how many parameters are passed, COUNTING the program name. So, if you pass 2 parameters like you want, argc will be 3 (but you should check for this, and print an error message if the user gives too many/too few params). argv[0], argv[1], and argv[2] will be strings (char *'s) to these parameters.

    To get an integer numeric input, use the atoi function, like this:

    Code:
    int main(int argc, char ** argv){
    	int param1, param2;
    	//other variables
    	if (argc < 3){
    		printf("Too few parameters!");
    		exit(1);
    	}
    	if(argc > 3){
    		printf("Too many parameters!");
    		exit(1);
    	}
    	//Ok, we have the right # of params:
    	param1 = atoi(argv[1]);
    	param2 = atoi(argv[2]);
    	//Rest of the code here
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM