Command-Line Arguments in C Programming
Command-line arguments allow a C program to accept input values at the time of execution, directly from the terminal or command prompt. These values can influence the behavior of the program without modifying the source code.This technique is widely used for writing utility programs, compilers, scripts, and tools that work with files, options, or data passed as input.
1. What Are Command-Line Arguments?
When you run a C program from the command line, you can pass values along with the program's name. These are called command-line arguments, and the program can use them just like regular variables.
Example:
./program input.txt output.txt
Here, input.txt
and output.txt
are command-line arguments.
2. main()
Function with Parameters
To access command-line arguments, the main()
function is defined with two parameters:
int main(int argc, char *argv[])
-
argc
: Argument Count- An integer that represents the number of arguments passed on the command line, including the program name itself.
-
argv
: Argument Vector- An array of character pointers (strings). Each
argv[i]
points to a command-line argument string. argv[0]
is always the name of the program (or the path used to run it).argv[1]
toargv[argc - 1]
contain the actual arguments passed by the user.
- An array of character pointers (strings). Each
3. Example Explanation of argc
and argv
Suppose the program is executed as:
./calc add 10 20
Then:
argc
= 4argv[0]
="./calc"
argv[1]
="add"
argv[2]
="10"
argv[3]
="20"
These string values can be accessed, parsed, and used inside the program.
4. Processing Command-Line Input
To make use of the command-line arguments:
- Loop through
argv[]
to access each argument. - Convert strings to appropriate types (e.g., integers using
atoi()
, floating-point usingatof()
). - Use conditions to check or validate the input.
Steps for Processing:
- Check argument count (
argc
) to ensure enough values are passed. - Access
argv[i]
for each argument. - Convert to appropriate data types if needed.
- Use the values in program logic.
5. Practical Use Cases
Use Case | Example |
---|---|
File operations | ./program input.txt output.txt |
Calculators or tools | ./calc multiply 10 5 |
Configurable execution | ./server --port 8080 --debug |
Automation scripts | ./compress file1.txt archive.zip |
6. Key Points to Remember
- The first element
argv[0]
is always the program name. - Always validate the number of arguments using
argc
before accessingargv[]
. - All command-line arguments are strings, and need to be converted before performing mathematical operations.
- Using command-line arguments makes a program more flexible and reusable.
7. Summary Table
Term | Meaning |
---|---|
argc |
Number of arguments passed (including program name) |
argv[] |
Array of strings representing each argument |
argv[0] |
Name of the program (path included) |
argv[1+] |
User-supplied command-line arguments |
Command-line arguments in C provide a powerful way to interact with a program at runtime. By using argc
and argv
, programmers can write more dynamic, adaptable, and automated code. This is especially useful in scripting, utilities, tools, and programs that require varying input parameters.