Command-Line Arguments in C

C Programming @ Freshers.in

Command-line arguments are a fundamental aspect of C programming, allowing developers to interact with their programs directly from the terminal or command prompt. In this article, we will delve into advanced topics related to command-line arguments in C. You will learn advanced techniques, best practices, and real-world examples with corresponding outputs, empowering you to harness the full potential of command-line arguments in your C programs.

Understanding Command-Line Arguments

Command-line arguments are parameters that you can pass to a C program when you run it from the command line. These arguments provide a way to customize program behavior, input, or execution. In C, you can access these arguments using the main function’s parameters.

Basic Usage of Command-Line Arguments

Let’s start with a basic example to refresh your knowledge of command-line arguments:

#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("Total arguments: %d\n", argc);
    for (int i = 0; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}

In this example, argc stores the number of command-line arguments, and argv is an array of strings containing those arguments. The program prints the total number of arguments and lists each argument.

Output (if you run the program with ./program arg1 arg2 arg3):

Total arguments: 4
Argument 0: ./program
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3

Advanced Topics in Command-Line Arguments

Parsing Numeric Arguments

You often need to convert command-line arguments to numeric values for further processing. Here’s how you can parse integer arguments:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <number>\n", argv[0]);
        return 1;
    }
    int number = atoi(argv[1]);
    printf("Double of %d is %d\n", number, 2 * number);
    return 0;
}

In this example, we expect a single numeric argument. If the argument count is not as expected, we print usage instructions. Otherwise, we convert the argument to an integer using atoi and perform a calculation.

Output (if you run the program with ./program 5):

Double of 5 is 10

Handling Flags and Options

Command-line arguments often include flags and options. Let’s implement a program that accepts a -v flag for verbose output:

#include <stdio.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
    bool verbose = false;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-v") == 0) {
            verbose = true;
        }
    }
    if (verbose) {
        printf("Verbose mode is enabled.\n");
    } else {
        printf("Verbose mode is disabled.\n");
    }
    return 0;
}

In this example, we iterate through the command-line arguments and set the verbose flag to true if we encounter the -v flag.

Output (if you run the program with ./program -v):

Verbose mode is enabled.
Author: user