Command Line Arguments
What are command line arguments?
Usually a program gets input with scanf while it runs. Command line arguments are different — you supply values as you launch the program, right after its name in the terminal:
./app hello 42
Here hello and 42 are arguments handed to the program. This is how tools like gcc file.c receive the file name. To catch them, main takes two parameters.
argc and argv
int main(int argc, char *argv[]) {
// argc = how many arguments (including program name)
// argv = array of the argument strings
}| Name | Meaning |
|---|---|
argc | Argument count — how many, including the program name |
argv[0] | The program's own name |
argv[1]... | The values you typed |
For ./app hello 42, argc is 3, argv[0] is "./app", argv[1] is "hello", and argv[2] is "42".
Printing the arguments
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("You passed %d arguments.\n", argc - 1);
for (int i = 1; i < argc; i++) // start at 1, skip program name
printf("argv[%d] = %s\n", i, argv[i]);
return 0;
}Output:
You passed 2 arguments.
argv[1] = hello
argv[2] = 42
The loop starts at 1 because argv[0] is just the program's name.
Turning arguments into numbers
Every argument is a string, even "42". To use it as a number, convert it with atoi (to int) or atof (to double) from <stdlib.h>.
#include <stdlib.h> int n = atoi(argv[1]); // "42" -> 42
A sum program
Add up all the numbers passed on the command line.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: ./app num1 num2 ...\n");
return 1;
}
int sum = 0;
for (int i = 1; i < argc; i++)
sum += atoi(argv[i]);
printf("Sum = %d\n", sum);
return 0;
}Output:
Sum = 60
Always confirm argc is large enough before touching argv[1], or a missing argument will read past the array.
Common mistakes
- Starting the loop at
0and accidentally including the program name. - Using an argument as a number without converting it with
atoi/atof. - Reading
argv[1]without checkingargc— crashes if it is missing. - Assuming arguments are validated — the user can type anything.
Write a program that takes two numbers on the command line and prints their product. Handle the case where fewer than two numbers are given by printing a usage message.
Summary
- Command line arguments are values passed when launching the program.
main(int argc, char *argv[])receives them.argccounts them;argv[0]is the program name, real values start atargv[1].- Arguments are strings — convert with
atoioratoffor numbers. - Always check
argcbefore reading an argument.
Frequently Asked Questions
What are command line arguments in C?
./app hello 42. C passes them into your program through two parameters of main — argc and argv — so the program can read and use them.What are argc and argv in C?
argc (argument count) is an integer holding how many arguments were passed, including the program name. argv (argument vector) is an array of strings holding the actual arguments, where argv[0] is the program name and argv[1] onward are the values you typed.What is stored in argv[0]?
argv[0] holds the name (or path) used to run the program itself. The real user-supplied arguments start at argv[1], which is why loops that process arguments usually begin at index 1.How do you convert a command line argument to a number?
<stdlib.h> — atoi for an int or atof for a floating-point value. For example atoi(argv[1]) turns the first argument into an integer.Why is argv an array of strings and not numbers?
42, it arrives as the characters "4" and "2", not the number 42. That is why you must convert arguments yourself when you need them as numbers.