Command-line arguments: argc & argv
7 min lesson
- Command-line arguments are the words you type after a program's name in a terminal, and C delivers them to main through argc and argv.
- They matter because many tools are driven by arguments: a file name to open, a number of iterations, or a flag to change behavior.
- Think of argc and argv as a shopping list handed to the program: argc is how many items are on the list, and argv is the list itself.
- argc is the count of arguments, including the program name, so it is always at least 1.
- argv is an array of strings: argv[0] is the program name, argv[1] is the first real argument, and argv[argc] is a NULL pointer.
- The main function signature for arguments is int main(int argc, char *argv[]).
- The computer builds this array from the shell before main runs, splitting the typed command on spaces.
- The syntax is: int main(int argc, char *argv[]) { ... }.
- A realistic use: a program that reads a file name from argv[1] and processes it, or a calculator that reads two numbers as arguments.
- A surprising detail: arguments are always strings, so you must convert them with strtol or atoi before using them as numbers.
- The example program prints each argument it received, using a fixed set of arguments to simulate a command line.
- Always check argc before using argv[1] or higher, or you risk reading beyond the array.
- Remember argv[0] is the program name, not the first argument.
- Convert numeric arguments with strtol or atoi before using them.
- Use argc to know how many arguments the user actually passed.
- Command-line tools are powerful. You are writing real utilities now!
#include <stdio.h>
int main(int argc, char *argv[]) {
// Print how many arguments were passed (including the program name)
printf("argc = %d\n", argc);
// Print each argument with its index
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
// Run with: ./app hello 42
// To make the example deterministic, the expected run uses two arguments.What happens: argc = 1 argv[0] = ./app
Breaking it down
main signature
int main(int argc, char *argv[])The standard form for arguments. argc is the count and argv is the array of strings.
argc
printf("%d", argc);The number of arguments including the program name. It is at least 1, because argv[0] always exists.
argv[i]
argv[0]A pointer to the i-th argument string. argv[0] is the program name; real arguments start at argv[1].
Loop over arguments
for (int i = 0; i < argc; i++)Visits every argument safely. Stopping at i < argc avoids reading past the last one.
argv contents
| Index | Contents | Example |
|---|---|---|
| argv[0] | Program name | ./app |
| argv[1] | First argument | hello |
| argv[2] | Second argument | 42 |
| argv[argc] | NULL | (end marker) |
A helpful analogy
argc and argv are like a shopping list handed to a shopper. argc is the number of items on the list, and argv is the list itself. The first item (argv[0]) is always the name of the shopper (the program), and the real items follow.
Nesting rules
- 1.Always check argc before reading argv[i] for i >= 1.
- 2.argv[0] is the program name, never the first user argument.
- 3.Arguments are strings; convert with strtol or atoi to use them as numbers.
- 4.argv[argc] is guaranteed to be NULL, which is handy for sentinel loops.
Common mistakes
Reading argv[1] without checking argc
Avoid
char *name = argv[1]; // may be missingUse instead
if (argc < 2) { printf("usage: app name\n"); return 1; }
char *name = argv[1];If the user passes no arguments, argv[1] is out of bounds. Always check argc first and print a usage message.
Treating argv[0] as the first user argument
Avoid
char *first = argv[0]; // program nameUse instead
char *first = argv[1];argv[0] is the program name. User arguments start at index 1.
Using an argument as an int directly
Avoid
int n = argv[1]; // wrong typeUse instead
int n = atoi(argv[1]);Arguments are strings. Assigning a char * to an int is a type error; convert with atoi or strtol.
Quick tips
- •Print a usage message when argc is too small so users know what to pass.
- •Use strtol instead of atoi when the number must be validated.
- •Try running the program with no arguments and with two to see how argc changes.
- •Use argv[argc] == NULL as a sentinel if you prefer not to use argc.
- •Quote arguments containing spaces on the shell: ./app "hello world".