Introduction to Command Line Arguments

Command line arguments in C allow users to pass inputs to a program at runtime through the command line. This feature enables flexible and dynamic program execution without modifying the source code.

Command Line Arguments in C
Command Line Arguments in C

Key Benefits of Using Command Line Arguments

  • User Input Handling: Provides input to the program without manual intervention.
  • Flexibility: Allows different inputs without recompiling the code.
  • Efficient Execution: Useful in scripting, automation, and batch processing.

Understanding Command Line Arguments

In C, the main() function can accept command line arguments through two parameters:

int main(int argc, char *argv[])
  • argc (Argument Count): Holds the number of arguments passed, including the program name.
  • argv (Argument Vector): An array of character pointers representing the arguments.

Example: Displaying 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;
}

Output (if executed as ./program Hello World) Click me!

Number of arguments: 3  
Argument 0: ./program  
Argument 1: Hello  
Argument 2: World  

Parsing Integer Arguments

Command line arguments are passed as strings. To use numeric values, they must be converted using atoi() or strtol().

Example: Adding Two Numbers

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

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

Execution Example

$ ./program 5 10  
Sum: 15  

Handling Command Line Options

Many programs use options (flags) for different functionalities.

Example: Using Flags (-v for version, -h for help) Click me!

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

int main(int argc, char *argv[]) {
    if (argc == 2) {
        if (strcmp(argv[1], "-h") == 0) {
            printf("Usage: ./program <options>\n-h : Help\n-v : Version\n");
        } else if (strcmp(argv[1], "-v") == 0) {
            printf("Program Version: 1.0\n");
        } else {
            printf("Invalid option. Use -h for help.\n");
        }
    } else {
        printf("No options provided. Use -h for help.\n");
    }
    return 0;
}

Best Practices for Command Line Arguments

  • Validate Inputs: Check argc before accessing argv[] to prevent segmentation faults.
  • Convert Properly: Use atoi() or strtol() for numeric inputs.
  • Provide Usage Instructions: Guide users with -h or --help.

Conclusion

Command line arguments in C enhance flexibility and user interaction. By understanding how to parse, validate, and utilize them effectively, developers can create dynamic and efficient programs.

Interview Questions

Q1: What is the purpose of argc and argv[] in C?

Company: Infosys
Answer:

  • argc stores the number of arguments, including the program name.
  • argv[] is an array of strings holding the arguments.
Q2: How do you convert a command line argument to an integer?

Company: TCS
Answer:
Use atoi() or strtol().

int num = atoi(argv[1]);
Q3: What happens if you access an out-of-bounds argument in argv[]?

Company: Cognizant
Answer:
Accessing beyond argc results in undefined behavior, possibly causing a segmentation fault.

Q4: Can command line arguments be used for file handling in C?

Company: Wipro
Answer:
Yes. Example: Opening a file passed via command line.

FILE *file = fopen(argv[1], "r");
Q5: What is the output of ./program Hello World if argc and argv are printed?

Company: HCL Technologies
Answer:

argc = 3  
argv[0] = ./program
argv[1] = Hello
argv[2] = World

Quizzes

Command Line Arguments in C Quiz

Leave a Reply

Your email address will not be published. Required fields are marked *