๐Ÿง  C Advanced Topics
Estimated reading: 3 minutes 7 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 :

Leave a Reply

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

Share

๐Ÿ–ฅ๏ธ C Command Line Arguments

Or Copy Link

CONTENTS
Scroll to Top