C - Command-Line Arguments in C
Command-line arguments in C allow a program to receive input directly when the program is started from the command line. Instead of asking the user to enter values after the program begins, information can be provided along with the program's name during execution.
This feature is especially useful for system programs, utilities, scripts, automation, and applications that need to receive configuration values or file names when they start.
1. What Are Command-Line Arguments?
Normally, a C program starts execution from the main() function:
int main()
{
// Program statements
}
When command-line arguments are required, the main() function can be written as:
int main(int argc, char *argv[])
{
// Program statements
}
Here:
-
argcstands for argument count. -
argvstands for argument vector. -
argctells us how many command-line arguments were supplied. -
argvis an array of strings containing those arguments.
For example, suppose a program is compiled as program and executed like this:
program John 25
The arguments are:
program
John
25
Therefore:
argc = 3
The argv array contains:
argv[0] = "program"
argv[1] = "John"
argv[2] = "25"
2. Understanding argc
argc is an integer that stores the total number of command-line arguments.
An important point is that the program name itself is counted as the first argument.
Consider:
student Rahul 21
There are three arguments:
argv[0] = "student"
argv[1] = "Rahul"
argv[2] = "21"
Therefore:
argc = 3
If the program is executed without any additional arguments:
student
then:
argc = 1
because argv[0] still contains the program name.
3. Understanding argv
argv is an array of character pointers. It stores the command-line arguments as strings.
Its declaration is commonly written as:
char *argv[]
It can also be written as:
char **argv
Both forms can be used for the main() function.
For example:
calculate 10 20
The values will be stored as:
argv[0] = "calculate"
argv[1] = "10"
argv[2] = "20"
Notice that even though 10 and 20 look like numbers, they are initially stored as strings.
4. Simple Command-Line Argument Program
The following program displays all command-line arguments:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Suppose the program is compiled as:
test
and executed as:
test Apple Mango Orange
The output will be:
Argument 0: test
Argument 1: Apple
Argument 2: Mango
Argument 3: Orange
Here:
argc = 4
because there are four arguments including the program name.
5. Using Command-Line Arguments as Numbers
Command-line arguments are received as strings. If you want to perform mathematical calculations with them, they must be converted into numeric values.
For example:
add 10 20
Here:
argv[1]
contains "10" and:
argv[2]
contains "20".
They are strings, so they cannot directly be added as integers.
The atoi() function from <stdlib.h> can be used to convert a string into an integer.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b, sum;
a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
If the program is executed as:
add 10 20
the output will be:
Sum = 30
6. Why argc Should Be Checked
A common mistake is to access argv[1] or argv[2] without checking whether those arguments actually exist.
For example:
a = atoi(argv[1]);
b = atoi(argv[2]);
This assumes that the user has supplied at least two additional arguments.
A safer program checks argc first:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b;
if (argc != 3)
{
printf("Usage: add number1 number2\n");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("Sum = %d\n", a + b);
return 0;
}
Now the program expects exactly three arguments:
argv[0] = program name
argv[1] = first number
argv[2] = second number
If the user executes:
add 15 25
the output is:
Sum = 40
If the user executes:
add 15
the program displays:
Usage: add number1 number2
This prevents the program from trying to access an argument that does not exist.
7. Example: Finding the Largest Number
Command-line arguments can be used to process multiple values.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int largest;
if (argc < 2)
{
printf("Please provide at least one number.\n");
return 1;
}
largest = atoi(argv[1]);
for (i = 2; i < argc; i++)
{
if (atoi(argv[i]) > largest)
{
largest = atoi(argv[i]);
}
}
printf("Largest number = %d\n", largest);
return 0;
}
Execution:
largest 15 42 8 27 63
Output:
Largest number = 63
The program reads each command-line argument, converts it to an integer, and compares it with the current largest value.
8. Command-Line Arguments and Strings
Command-line arguments are particularly useful when programs need to work with file names or text.
Example:
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: program filename\n");
return 1;
}
printf("File selected: %s\n", argv[1]);
return 0;
}
Execution:
program report.txt
Output:
File selected: report.txt
Here, argv[1] contains the file name.
This approach is commonly used in programs that process files, configuration values, search terms, and other text-based input.
9. Multiple Command-Line Arguments
A program can accept many command-line arguments.
For example:
student Rahul 21 Bangalore ComputerScience
The arguments are:
argv[0] = "student"
argv[1] = "Rahul"
argv[2] = "21"
argv[3] = "Bangalore"
argv[4] = "ComputerScience"
Therefore:
argc = 5
The program can access each value individually or process them using a loop.
10. Important Characteristics
Command-line arguments have several important characteristics:
They are available when the program starts.
The user does not have to enter them after program execution begins.
They are stored as strings.
Even numeric-looking arguments such as "100" are initially character strings.
The program name is normally stored in argv[0].
The number of arguments is stored in argc.
Arguments can be processed individually.
For example, argv[1], argv[2], and argv[3] represent different arguments.
They are useful for automation.
A program can be executed repeatedly with different inputs without requiring interactive input.
11. Command-Line Arguments vs scanf()
There is an important difference between command-line arguments and scanf().
With scanf():
int age;
printf("Enter age: ");
scanf("%d", &age);
The program starts first and then waits for the user to enter the age.
With command-line arguments:
program 25
the value is supplied when the program is launched.
Command-line arguments are therefore useful when input needs to be supplied automatically or when a program is being executed from another program, script, or terminal command.
12. Advantages of Command-Line Arguments
Command-line arguments provide several advantages:
-
They allow input to be supplied when a program starts.
-
They reduce the need for interactive input.
-
They are useful for automation and scripting.
-
They make programs convenient to use from a terminal.
-
They allow programs to receive file names, options, and configuration values.
-
They are useful for system utilities and command-line applications.
-
They allow the same executable to be run with different inputs.
13. Limitations and Precautions
Since command-line arguments are strings, numeric values generally need conversion before mathematical operations.
The program should also validate argc before accessing an element of argv. Accessing an unavailable argument can lead to undefined behavior.
For simple integer conversion, atoi() is commonly demonstrated in beginner programs. For more robust programs, functions such as strtol() are preferable because they provide better error handling and validation.
Conclusion
Command-line arguments provide a way for a C program to receive information directly when it is launched. The argc parameter tells the program how many arguments were supplied, while argv provides access to the individual argument strings.
The basic structure is:
int main(int argc, char *argv[])
{
// program code
}
The most important points to remember are:
argc → Number of command-line arguments
argv → Array containing the arguments
argv[0] → Program name
argv[1] → First user-supplied argument
argv[2] → Second user-supplied argument
Understanding command-line arguments is an important step toward developing practical C programs that can interact with terminals, scripts, files, and other software.