๐ฅ๏ธ 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[])
Parameter | Meaning |
---|---|
argc | Argument 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
Application | Example |
---|---|
CLI tools | grep pattern filename.txt |
Compilers | gcc 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 :