Command Line Arguments (argc/argv)

Learn how to pass parameters to your program at runtime.

📖 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:
VariableValueMeaning
argc33 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.
FunctionTarget typeHeaderExample
atoi(s)intstdlib.hint n = atoi(argv[1]);
atof(s)doublestdlib.hdouble d = atof(argv[1]);
atol(s)longstdlib.hlong 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.
Lessons to revisit: For input, see Lesson 5: scanf ; for function parameters, see Lesson 26: Function basics ; for files, see Lesson 41: File input and output .

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 +.

Related Lessons

Functions
Lesson 26: Functions (Basics)
Review the basics of function arguments and return values, including the main function.
Advanced
Lesson 41: File I/O
Leads into practical examples that read and write files whose names are passed as arguments.
Professional
Multi-file Projects & Makefile
Practice the workflow of building and running programs from the command line.
← Previous lesson
Lesson 34: Recursion
Next lesson →
Lesson 36: Pointer Basics
Share this article
Share on X Share on Facebook