Home › Functions › Command Line Arguments
📖 What to learn on this page
✅ Must-know essentials
argc is the number of arguments, including the program name
argv[0] is the program name; user arguments start at argv[1]
Arguments are strings, so convert them before using them as numbers
⭐ Read if you have time
Always check argc for missing arguments
Use strtol for stricter conversion than atoi
Apply it to real programs that take file names or options
What are argc and argv?
Until now, you've been writing int main(void). But main can also be written to accept arguments from the command line.
int main (int argc, char *argv[]) {
// argc: number of arguments (Argument Count)
// argv: array of argument strings (Argument Vector)
}
argc
The number of arguments (including the program name itself ).
argv[]
Each argument as a string — an array of char*.
For example, if you run ./myprogram hello world:
Variable Value Meaning
argc 3 3 arguments total
argv[0] "./myprogram" the program name
argv[1] "hello" first argument
argv[2] "world" second argument
Important: argv[0] holds the program's own name . The arguments supplied by the user start at argv[1] .
Basic Usage
Here's a program that prints every argument it receives:
#include <stdio.h>
int main (int argc, char *argv[]) {
printf ("Number of arguments: %d\n" , argc);
for (int i = 0 ; i < argc; i++) {
printf ("argv[%d] = %s\n" , i, argv[i]);
}
return 0 ;
}
$ gcc args.c -o args
$ ./args hello world 123
Number of arguments: 4
argv[0] = ./args
argv[1] = hello
argv[2] = world
argv[3] = 123
Using Arguments as Numbers
Every element of argv is a string (a char*) . To use one as a number, you have to convert it first.
Function Target type Header Example
atoi(s)int stdlib.h int n = atoi(argv[1]);
atof(s)double stdlib.h double d = atof(argv[1]);
atol(s)long stdlib.h long l = atol(argv[1]);
Example: command-line calculator
#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 ("%d + %d = %d\n" , a, b, a + b);
return 0 ;
}
$ ./calc 15 27
15 + 27 = 42
Always validate your arguments. Check argc first, and if the count is wrong, print a usage message and exit.
Practical Example: Passing a File Name
Real-world programs often take the name of a file to process as a command-line argument.
#include <stdio.h>
int main (int argc, char *argv[]) {
if (argc != 2 ) {
printf ("Usage: %s filename\n" , argv[0 ]);
return 1 ;
}
FILE *fp = fopen (argv[1 ], "r" );
if (fp == NULL ) {
printf ("Cannot open file '%s'\n" , argv[1 ]);
return 1 ;
}
char line[256 ];
while (fgets (line, 256 , fp) != NULL ) {
printf ("%s" , line);
}
fclose (fp);
return 0 ;
}
$ ./cat scores.txt
Taro 80
Hanako 95
Jiro 70
This is a simplified version of the classic UNIX cat command.
Try it yourself: argc and argv
Practice passing values from outside the program at startup. For every exercise, check argc first.
Task 1: Personalized greeting
Run ./hello Taro to print Hello, Taro!. Show usage instructions when the argument is missing.
Task 2: Addition command
./add 12 30 should print 42. atoi converts the text arguments to integers.
Task 3: cat with line numbers
Accept one filename and print each line preceded by its line number. fopen must also be checked for failure.
Review Quiz
// When you run the following command
// $ ./prog hello world
What is the value of argc?
2
3
1
0
Explanation: argc counts the arguments including the program name : ./prog, hello, world — that's 3 . So argv[0]="./prog", argv[1]="hello", argv[2]="world".
// Running $ ./calc 10 20
int a = atoi (argv[1 ]);
int b = atoi (argv[2 ]);
printf ("%d\n" , a + b);
What does it print?
30
1020
error
0
Explanation: atoi("10") produces 10 and atoi("20") produces 20, so the sum is 30 . argv entries are strings; convert them to numbers before arithmetic. C does not concatenate strings with +.