๐Ÿง  C Advanced Topics
Estimated reading: 3 minutes 189 views

C Command Line Arguments โ€“ Pass Parameters to main()


Introduction โ€“ What Are Command Line Arguments in C?

In C programming, command line arguments allow users to pass input parameters to a program directly from the terminal or command line. This enables a program to behave dynamically based on external inputsโ€”without needing interactive prompts or hardcoded values.

In this guide, youโ€™ll learn:

  • How command line arguments work in C
  • Syntax of main() with arguments
  • How to access, use, and validate input
  • Real-world use cases for argument-based programs

Command Line Syntax in C

To use command line arguments, the main() function is declared with two parameters:

int main(int argc, char *argv[])
ParameterMeaning
argcArgument count (number of arguments, including program name)
argv[]Argument vector (array of strings passed to the program)

argv[0] always contains the program name or path.


Basic Example

C Program: args.c

#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;
}

Compilation and Execution

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

Output:

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

Use Cases for Command Line Arguments

ApplicationExample
CLI toolsgrep pattern filename.txt
Compilersgcc main.c -o app
Custom flags./app -v --help
Input/output files./convert input.png output.jpg
Scripting/batching./backup /home/user

Input Parsing & Validation Example

#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 a = atoi(argv[1]);
    int b = atoi(argv[2]);
    printf("Sum = %d\n", a + b);

    return 0;
}

This version checks the number of arguments and uses atoi() to convert strings to integers.


Best Practices & Tips

Best Practice:
Always validate the value of argc before accessing argv[] elements to avoid out-of-bounds errors.

Tip:
Use atoi(), atof(), or strtol() to convert argument strings to numbers safely.

Pitfall:
argv[] elements are stringsโ€”never use them as numbers directly without conversion.


Summary โ€“ Recap & Next Steps

Command line arguments are a key feature for building interactive and configurable C programs. They let your programs receive external input directly from shell scripts, terminals, or batch jobs.

Key Takeaways:

  • Use int main(int argc, char *argv[]) to access arguments
  • argv[0] is the program name
  • Convert argv[n] strings to integers/floats using library functions
  • Validate argc to ensure correct input count

Real-World Relevance:

Used in compilers, shell utilities, build tools, file processors, and scriptable automation.


Frequently Asked Questions (FAQ)

What is argc and argv[]?

argc is the number of command line arguments, argv[] is an array of strings containing those arguments.


What is stored in argv[0]?

The name (or path) of the executed program.


How do I convert argv[] to an integer?

Use atoi() or strtol() from <stdlib.h>:

int x = atoi(argv[1]);

What happens if I access argv[3] when argc is 2?

This results in undefined behavior and likely a segmentation fault. Always check argc first.


Can I pass arguments when using an IDE?

Yes. Most IDEs have a section under project/run settings where you can specify command line arguments.


Share Now :
Share

๐Ÿ–ฅ๏ธ C Command Line Arguments

Or Copy Link

CONTENTS
Scroll to Top