Introduction:
Command-line arguments in C programming provide a convenient way to pass inputs to a program directly from the terminal. They allow developers to customize the behavior of a program without modifying its source code. In this blog post, we’ll explore the concept of command-line arguments, how to use them, access them in C programs, and provide practical examples to illustrate their usage.

Understanding Command-line Arguments:

  1. What are Command-line Arguments?
   ./program_name arg1 arg2 arg3 ...
  1. Accessing Command-line Arguments:
   int main(int argc, char *argv[]) {
       // argc: Number of arguments
       // argv: Array of strings containing the arguments

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

       return 0;
   }
  1. argc and argv:

Example: Simple Calculator with Command-line Arguments

Let’s create a simple calculator program that takes two numbers and an operation as command-line arguments.

#include <stdio.h>
#include <stdlib.h> // for atoi

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Usage: %s num1 num2 operation\n", argv[0]);
        printf("Operations: add, subtract, multiply, divide\n");
        return 1;
    }

    int num1 = atoi(argv[1]);
    int num2 = atoi(argv[2]);
    char *operation = argv[3];

    int result;
    if (strcmp(operation, "add") == 0) {
        result = num1 + num2;
    } else if (strcmp(operation, "subtract") == 0) {
        result = num1 - num2;
    } else if (strcmp(operation, "multiply") == 0) {
        result = num1 * num2;
    } else if (strcmp(operation, "divide") == 0) {
        if (num2 != 0) {
            result = num1 / num2;
        } else {
            printf("Error: Division by zero\n");
            return 1;
        }
    } else {
        printf("Invalid operation: %s\n", operation);
        return 1;
    }

    printf("Result: %d\n", result);
    return 0;
}

Executing the Program:

  ./calculator 10 5 add
  ./calculator 20 3 subtract
  ./calculator 8 4 multiply
  ./calculator 12 4 divide

Conclusion:
Command-line arguments in C programming provide a flexible and powerful way to customize program behavior based on user input from the terminal. By understanding how to access argc and argv, developers can create programs that accept and process command-line arguments efficiently. The practical example of a simple calculator demonstrates how command-line arguments can be used to perform different operations on numbers without the need for interactive inputs. Experiment with different scenarios, explore additional functionalities, and apply these concepts to your programming projects. Mastering command-line arguments in C will enable you to create versatile and user-friendly applications that can be controlled directly from the terminal.

Leave a Reply