Question

What `int argc, char *argv[]` Means in C++ main()

cppparameterscommand-line-argumentsargvargc

Question

In many C++ IDEs and compilers, the generated main function often looks like this:

int main(int argc, char *argv[])

When writing C++ manually with a command-line compiler, I often use:

int main()

What do argc and argv mean, and are they necessary for every program?

Short Answer

By the end of this page, you will understand how C++ programs can receive command-line arguments through main, what argc and argv represent, when they are useful, and when you can safely ignore them. You will also see practical examples of reading values passed to a program from the terminal.

Concept

In C++, main is the entry point of a program. It is the function where execution begins.

A common form of main is:

int main(int argc, char *argv[])

These parameters allow your program to receive command-line arguments.

  • argc stands for argument count
  • argv stands for argument vector

Together, they let your program read words or values that were typed after the program name when it was started from the command line.

For example, if you run:

./app hello 123

then your program receives:

  • argc = 3
  • argv[0] = "./app"
  • argv[1] = "hello"
  • argv[2] = "123"

Why this matters

Command-line arguments are useful when you want to:

  • pass input files to a program
  • choose a mode like --debug
  • provide user settings without changing the code
  • automate scripts and tools

Are they required?

No. They are not required for every program.

If your program does not need input from the command line, you do not need to use argc and argv.

However, in standard C++, the usual valid forms are:

int main()

or

int main(int argc, char* argv[])

Many developers prefer the second form in templates because it gives flexibility if command-line arguments are needed later.

Mental Model

Think of your program like a machine that can be started with a small note attached.

  • main() means the machine starts with no extra note.
  • main(int argc, char *argv[]) means the machine starts with a list of notes.

argc tells you how many notes there are.

argv gives you the actual notes.

So if someone starts the machine like this:

./tool report.txt --verbose

then:

  • argc tells you there are 3 pieces of text
  • argv lets you read each one by position

You can think of argv as an array of strings, and argc as the size of that array.

Take Quiz

Syntax and Examples

The common syntax is:

int main(int argc, char* argv[])
{
    // program code
}

You may also see this equivalent form:

int main(int argc, char** argv)
{
    // program code
}

Both forms are commonly used for main parameters.

Simple example

#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "Number of arguments: " << argc << "\n";

    for (int i = 0; i < argc; i++)
    {
        std::cout << "argv[" << i << "] = " << argv[i] << "\n";
    }

    return 0;
}

If the program is run as:

./app apple banana

possible output is:

Number of arguments: 3
argv[0] = ./app
argv[1] = apple
argv[2] = banana

Important detail

argv[0] is usually the program name or path used to run the program. That is why argc is at least 1 in normal cases.

Example: expecting one value

#include <iostream>

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        std::cout << "Usage: ./app <name>\n";
        return 1;
    }

    std::cout << "Hello, " << argv[1] << "!\n";
    return 0;
}

Run it like this:

./app Alice

Output:

Hello, Alice!

This example shows why argc is useful: it helps prevent accessing arguments that were not provided.

Step by Step Execution

Consider this program:

#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "argc = " << argc << "\n";

    if (argc > 1)
    {
        std::cout << "First user argument: " << argv[1] << "\n";
    }
    else
    {
        std::cout << "No extra argument provided.\n";
    }

    return 0;
}

Suppose you run:

./app test

Step by step

  1. The operating system starts the program.
  2. It passes the command-line text into main.
  3. argc becomes 2 because there are two items:
    • argv[0] = "./app"
    • argv[1] = "test"
  4. The program prints:
argc = 2
  1. The condition argc > 1 is true.
  2. The program prints:
First user argument: test
  1. return 0; signals successful completion.

If you run without extra arguments

./app

Then:

  • argc = 1
  • argv[0] = "./app"
  • there is no argv[1]

So the program safely prints:

No extra argument provided.

Real World Use Cases

Command-line arguments are very common in real programs.

1. File processing tools

A program may accept a filename:

./reader data.txt

Then argv[1] contains data.txt.

2. Flags and modes

Programs often support options like:

./server --debug
./build --release

These flags change behavior without editing the source code.

3. Automation scripts

When a program is called from a script, arguments let the script control it:

./resize image.png 800 600

4. Test runners and developer tools

Many development tools depend heavily on command-line arguments:

./tests --filter login

5. Configuration at startup

Small utilities often read startup settings this way instead of asking interactively.

Take Quiz

Real Codebase Usage

In real codebases, developers usually do more than just print argv.

Common patterns

Guard clauses

Check that the required arguments were provided before continuing.

if (argc < 2)
{
    std::cerr << "Usage: ./app <filename>\n";
    return 1;
}

This avoids invalid memory access like reading argv[1] when it does not exist.

Early returns for invalid input

If the user passes bad arguments, exit quickly with a helpful message.

if (argc != 3)
{
    std::cerr << "Expected exactly 2 arguments.\n";
    return 1;
}

Parsing values

Arguments are strings, so developers convert them when needed.

#include <string>

int count = std::stoi(argv[1]);

Reading flags

Programs often compare arguments to known options.

#include <string>

if (std::string(argv[1]) == "--help")
{
    std::cout << "Show help here.\n";
    return 0;
}

Looping through all arguments

Useful for programs that accept many options.

for (int i = 1; i < argc; i++)
{
    std::cout << "Argument: " << argv[i] << "\n";
}

Why teams use this pattern

  • easy to automate
  • easy to test
  • works well in scripts and CI pipelines
  • avoids hardcoding settings

Common Mistakes

1. Accessing argv[1] without checking argc

This is one of the most common mistakes.

Broken code:

#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << argv[1] << "\n";
    return 0;
}

If no argument is passed, argv[1] does not exist.

Better:

#include <iostream>

int main(int argc, char* argv[])
{
    if (argc > 1)
    {
        std::cout << argv[1] << "\n";
    }
    else
    {
        std::cout << "No argument given.\n";
    }

    return 0;
}

2. Forgetting that arguments are strings

Even if the user types a number, argv[1] is still text.

Broken idea:

int x = argv[1];

Better:

int x = std::stoi(argv[1]);

3. Assuming argv[0] is the first user input

argv[0] usually contains the program name or path, not the first extra argument.

  • argv[0] -> program name/path
  • argv[1] -> first user argument

4. Thinking int main() and void main() are the same

They are not.

Use:

int main()

or

int main(int argc, char* argv[])

Avoid:

void main()

void main() is not standard C++.

5. Using command-line arguments when interactive input would be better

Sometimes the input belongs in std::cin instead. Use argc/argv for startup parameters, not for every kind of user interaction.

Comparisons

FormMeaningWhen to use
int main()Program starts with no command-line parameters exposed in the function signatureSimple programs that do not need startup arguments
int main(int argc, char* argv[])Program can read command-line argumentsTools, scripts, configurable programs
int main(int argc, char** argv)Equivalent to the previous formSame use case; just a different pointer style

argc vs argv

ItemTypePurpose
argcintNumber of command-line arguments
argvchar*[]Array of C-style strings containing the arguments

Command-line arguments vs std::cin

ApproachBest for
argc and argvStartup options, filenames, flags, script automation
std::cinInteractive input while the program is running

char* argv[] vs modern C++ strings

argv uses C-style strings because that is how program arguments are traditionally passed to main. In modern C++, developers often convert them to std::string for easier use.

#include <string>

std::string firstArg = argv[1];
Take Quiz

Cheat Sheet

int main()
  • Use when no command-line arguments are needed.
int main(int argc, char* argv[])
  • Use when the program should read command-line arguments.

Key facts

  • argc = number of arguments
  • argv = array of argument strings
  • argv[0] is usually the program name/path
  • First user-supplied argument is usually argv[1]
  • Always check argc before accessing argv[n]
  • argv elements are C-style strings (char*)
  • Convert to std::string or numeric types if needed

Common patterns

Check for required argument:

if (argc < 2)
{
    std::cerr << "Usage: ./app <value>\n";
    return 1;
}

Loop through arguments:

for (int i = 0; i < argc; i++)
{
    std::cout << argv[i] << "\n";
}

Convert string to integer:

int value = std::stoi(argv[1]);

Edge cases

  • If no extra values are passed, argc is usually 1
  • Do not assume argv[1] exists
  • Do not use void main() in standard C++

FAQ

Is argc always at least 1?

Usually yes, because argv[0] normally contains the program name or path used to run the program.

Do I have to use argc and argv in every C++ program?

No. Use them only when your program needs command-line arguments.

What type is argv in C++?

It is commonly written as char* argv[], which is an array of C-style strings. It can also be written as char** argv.

What does argv[0] contain?

It usually contains the program name or path, not the first user argument.

Can I use std::string instead of char* argv[] in main?

Not directly in the standard main signature. Usually, you receive char* argv[] and convert elements to std::string.

Is int main() valid in C++?

Yes. It is a standard form of main when you do not need command-line arguments.

Why does main return int?

The returned integer tells the operating system whether the program finished successfully. 0 usually means success.

When should I prefer command-line arguments over std::cin?

Use command-line arguments for startup settings, file names, and automation. Use std::cin for interactive input during execution.

Take Quiz

Mini Project

Description

Build a small command-line greeter that reads a user's name from the terminal. This project demonstrates how argc and argv are used to accept startup input safely and produce different behavior depending on whether an argument was provided.

Goal

Create a C++ program that greets the user by name when a command-line argument is given, and shows a usage message when it is missing.

Requirements

  • Use main(int argc, char* argv[]).
  • Check whether a name argument was provided.
  • Print a greeting using the provided name.
  • Show a helpful usage message if no name is passed.
  • Return 0 on success and a non-zero value on incorrect usage.
Take Quiz

Keep learning

Related questions

View more