Arguments to main() in C

Sometimes we want to pass information into a program from the OS. Generally, we pass information to the main() function via command line arguments. A command line argument is the information we place after the program’s name when starting the program from the command line.

There are two special arguments, argc and argv, that are used to receive command line arguments. The argc parameter is an int value that holds the number of arguments being passed to main(). The argv parameter is a pointer to an array of character pointers. Each element in this array points to a command line argument, which is a string of char values.

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

int main(int argc, char **argv){
        
        if(argc!=2){
            printf("You need to provide your name as an argument.\n");
            exit(1);
        } else {
            //name is the second value pointed to
            //by argv
            printf("Hello %s\n", *(argv+1));
        }
        
    return 0;
    
}

Note that each command line argument should be separated by a space or a tab. We can provide a string with spaces as a single argument by enclosing in quotation marks. For instance, when running the program above from the command line, I entered ./exe “Al Jensen” to have my full name printed.

The most common way to declare argv is char *argv[]. The empty brackets indicate that the array’s length is unknown. We can access the individual arguments either via array indexing or pointer arithmetic. The following program will print out every argument sent to it from the command line.

#include <stdio.h>

int main(int argc, char *argv[]){
    
    int i = 0;
    
    /*
     * print out all the arguments received
     * from the OS
     */
    while(i<argc){
        /*
         * offset i by + 1
         * so arguments aren't numbered
         * from zero
         */
        printf("argument %d: \t %s\n",
                i+1, argv[i]);
        i++;
        
    }
    
    return 0;
    
}

Here is another program example, which counts down to ignition. The count is specified by the user on the command line. The atoi() function is used to convert the string value passed in from the OS to a integer value.

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

int main(int argc, char *argv[]){
    
    int count;
    
    /*
     * check to make sure enough arguments have
     * been provided
     */
    if(argc<2){
        printf("Please enter the length of the countdown.\n");
        exit(1);
    }
    
    /*
     * use atoi function to convert
     * string to integer
     */
    for(count=atoi(argv[1]); count; count--){
        printf("%d... \t", count);
    }
    
    printf("\nBlastoff!!!\n");
    
    return 0;
}

To access an individual character in one of the command line aguments, we add a second index to argv.

#include <stdio.h>

int main(int argc, char *argv[]){
    
    int i, j;
    
    for(i=0, j=0; i<argc; i++){
        while(argv[i][j]){
            printf("[%c]", argv[i][j++]);
        }
        putchar('\n');
        j = 0;
    }
    
    
    i=0;    
    
    /*
     * using pointer arithmetic to accomplish
     * the same thing
     */
    while(i<argc){
        j=0;
        printf("%s\n", *(argv+i));
        while(*(*(argv+i)+j)){
            printf("-%c-", *(*(argv+i)+j));
            j++;
        }
        i++;
        putchar('\n');
    }
    
    return 0;

}

Remember that for argv the first index access the entire string, and the second index accesses the individual char elements.

Interested in learning more? Get over 200 example programs for your Amazon Kindle or Kindle app: http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s