Question
What `int argc, char *argv[]` Means in C++ main()
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.
argcstands for argument countargvstands 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 = 3argv[0]="./app"argv[1]="hello"argv[2]="123"
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:
argctells you there are 3 pieces of textargvlets you read each one by position
You can think of argv as an array of strings, and argc as the size of that array.
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:
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
- The operating system starts the program.
- It passes the command-line text into
main. argcbecomes2because there are two items:argv[0]="./app"argv[1]="test"
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.
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.
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
Comparisons
| Form | Meaning | When to use |
|---|---|---|
int main() | Program starts with no command-line parameters exposed in the function signature | Simple programs that do not need startup arguments |
int main(int argc, char* argv[]) | Program can read command-line arguments | Tools, scripts, configurable programs |
int main(int argc, char** argv) | Equivalent to the previous form | Same use case; just a different pointer style |
argc vs argv
| Item | Type | Purpose |
|---|---|---|
argc |
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 argumentsargv= array of argument stringsargv[0]is usually the program name/path- First user-supplied argument is usually
argv[1] - Always check
argcbefore accessingargv[n] argvelements are C-style strings (char*)- Convert to
std::stringor numeric types if needed
Common patterns
Check for required argument:
(argc < )
{
std::cerr << ;
;
}
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++?
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
0on success and a non-zero value on incorrect usage.
Keep learning
Related questions
Advantages of Brace Initialization in C++
Learn why C++ brace initialization is often clearer and safer than other object initialization styles, with examples and common pitfalls.
Basic Rules and Idioms for Operator Overloading in C++
Learn the core rules, syntax, and common idioms for operator overloading in C++, including member vs non-member operators.
C++ Aggregates, Trivial Types, Trivially Copyable Types, and PODs Explained
Learn what aggregates, trivial types, trivially copyable types, and PODs mean in C++, how they differ, and why they matter.