🔴 Advanced  ·  Lesson 44

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:

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

C Language
int main(int argc, char *argv[]) {
    // argc = how many arguments (including program name)
    // argv = array of the argument strings
}
NameMeaning
argcArgument 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

C Language
#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;
}
Run: ./app hello 42
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>.

C Language
#include <stdlib.h>
int n = atoi(argv[1]);   // "42" -> 42

A sum program

Add up all the numbers passed on the command line.

C Language
#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;
}
Run: ./app 10 20 30
Output:
Sum = 60
💡 Check argc first

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 0 and accidentally including the program name.
  • Using an argument as a number without converting it with atoi/atof.
  • Reading argv[1] without checking argccrashes if it is missing.
  • Assuming arguments are validated — the user can type anything.
🏋️ Practice

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.
  • argc counts them; argv[0] is the program name, real values start at argv[1].
  • Arguments are strings — convert with atoi or atof for numbers.
  • Always check argc before reading an argument.

Frequently Asked Questions

What are command line arguments in C?
Command line arguments are values you type after the program name when running it from a terminal, such as ./app hello 42. C passes them into your program through two parameters of mainargc 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?
Command line arguments always arrive as strings, so you convert them with functions from <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?
Because the terminal passes everything as text. Even if you type 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.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.