C language content

In C, command line arguments allow users to pass information to a program at the time of execution. This can be particularly useful for customizing program behavior without hardcoding values. The main function in C can be defined to accept command line arguments as follows:

				
					int main(int argc, char *argv[])

				
			

Here’s a detailed explanation of these parameters:

  1. int argc: This argument stands for “argument count.” It represents the number of command line arguments passed to the program, including the program’s name itself. So if the program is called with no arguments, ‘argc‘ will be 1.

  2. char *argv[]: This is an array of strings (character pointers). Each element in the array is one of the command line arguments. ‘argv[0]‘ is the name of the program, and subsequent elements (‘argv[1]‘ through ‘argv[argc-1]‘) are the additional arguments passed to the program.

Example Program

Let’s consider a simple example that prints the command line arguments:

				
					#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Number of arguments: %d\n", argc);
    for (int i = 0; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}

				
			

Compilation and Execution

Suppose this code is saved in a file named args.c. You would compile and run it from the command line as follows:

				
					gcc -o args args.c
./args hello world

				
			

The output would be:

				
					Number of arguments: 3
Argument 0: ./args
Argument 1: hello
Argument 2: world

				
			

Parsing Arguments

Often, you’ll want to do more than just print the arguments; you might need to interpret them as numbers, options, or flags. For example:

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

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <num1> <num2>\n", argv[0]);
        return 1;
    }

    int num1 = atoi(argv[1]);
    int num2 = atoi(argv[2]);

    printf("%d + %d = %d\n", num1, num2, num1 + num2);

    return 0;
}

				
			

Error Handling

When dealing with command line arguments, it’s important to handle errors gracefully, such as ensuring the correct number of arguments are provided and that they are of the expected type.

Advanced Usage

In more complex programs, you might want to use libraries like getopt to handle command line arguments. getopt helps parse command line options more conveniently, especially when dealing with optional and mandatory arguments, short and long options, etc.

Example with getopt

Here’s a simple example using getopt:

				
					#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int opt;
    while ((opt = getopt(argc, argv, "a:b:")) != -1) {
        switch (opt) {
            case 'a':
                printf("Option -a with value %s\n", optarg);
                break;
            case 'b':
                printf("Option -b with value %s\n", optarg);
                break;
            default:
                fprintf(stderr, "Usage: %s -a value -b value\n", argv[0]);
                return 1;
        }
    }
    return 0;
}

				
			

This example accepts options ‘-a‘ and ‘-b‘ each requiring a value. If you run:

				
					./program -a 10 -b 20

				
			

The output would be:

				
					Option -a with value 10
Option -b with value 20

				
			

Summary

  • argc counts the arguments.
  • argv is an array of arguments.
  • Error handling is crucial for robustness.
  • Libraries like getopt can simplify argument parsing for complex scenarios.
Scroll to Top