Command Line Arguments in C

  • It is possible to pass some values from the commander prompt to our C program when they are executed. The values which are given at the command line are called command line arguments.
  • The command line arguments in C are handled by the main function. The two parameters that we use in the main function are arg c, arg v.
  • arg c is used to count the number of arguments.
  • arg v is a pointer character array that is used to store the arguments.
  • arg v(0) always contains the executable file name. The remaining arguments are stored from arg v(1) onwards.

Write a Programme to add two numbers using command line arguments in C.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    // Check for correct number of arguments
    if (argc != 3) {
        fprintf(stderr, "Usage: %s num1 num2\n", argv[0]);  // Error message to stderr
        return 1; // Indicate failure 
    }
    // Convert command-line arguments to integers
    int num1 = atoi(argv[1]);  // Use atoi for string-to-integer conversion
    int num2 = atoi(argv[2]);
    // Calculate and print the sum
    int sum = num1 + num2;
    printf("%d + %d = %d\n", num1, num2, sum);
    return 0; // Indicate successful execution
}

Output:

Usage: ./a.out num1 num2

 Key properties of Command-line arguments in C:

  1. Passed to the main Function: Command-line arguments are provided to a C program when it is executed and are received by the main function.
  2. Argument Count (argc) and Argument Vector (argv):
    • argc (argument count) is an integer that represents the number of arguments passed, including the program name itself.
    • argv (argument vector) is an array of character pointers (strings), where each element points to an individual argument. argv[0] typically holds the program name, argv[1] the first argument, and so on.
  3. String Representation: All command-line arguments are treated as strings, even if they represent numerical values.
  4. Null-Terminated Array: The argv array is null-terminated, meaning argv[argc] will always be a null pointer.
  5. External Control: Command-line arguments enable control of a program’s behavior from the external environment, making it more flexible and adaptable to different scenarios.

Advantages of Command Line Arguments in C

  1. Flexibility: Command-line arguments enable a program to behave differently based on external input, making it adaptable to various scenarios without modifying the code itself.
  2. Reusability: A single program can be reused for multiple purposes by providing different arguments, enhancing its versatility and saving development effort.
  3. Automation: Command-line arguments facilitate the creation of scripts and batch files to automate repetitive tasks, improving efficiency.
  4. External Control: Programs can be controlled from outside the code, offering a convenient way to configure settings or pass data without recompiling.
  5. Simplified Testing and Debugging: By easily changing input values through arguments, testing and debugging become more straightforward and less time-consuming.

Disadvantages of Command Line Arguments in C

  1. Steeper Learning Curve: Users need to understand command syntax and options, which can be challenging for beginners compared to graphical interfaces.
  2. Limited User-Friendliness: Command-line interfaces lack visual cues and are less intuitive than graphical interfaces, making them less approachable for non-technical users.
  3. Prone to Errors: Typos or incorrect syntax can lead to unexpected results or program failures, requiring careful input and attention to detail.
  4. Restricted Data Input: Complex data structures or large amounts of input may be difficult to manage solely through command-line arguments.
  5. Less Interactive: Unlike graphical interfaces, command-line interfaces offer limited real-time feedback and interaction during program execution.

Scroll to Top