How to read command-line arguments in C ? (argc, argv tutorial)
The main function in C receives command line parameters through argc and argv: argc is the number of parameters (at least 1, including the program name), argv is an array of pointers pointing to each parameter string, argv[argc] is a null pointer, and argc must be checked before accessing to prevent out-of-bounds.

In C , command-line arguments are passed to the main function via two parameters: argc (argument count) and argv (argument vector). argc is an integer telling you how many arguments were supplied, including the program name. argv is an array of C-style strings ( char* ) pointing to each argument.
Understanding argc and argv
argc is always at least 1 — because argv[0] holds the program's name or path (as invoked). Each subsequent element ( argv[1] , argv[2] , etc.) corresponds to the arguments typed after the program name. The array ends with a null pointer ( argv[argc] == nullptr ), so it's safe to loop from 0 to argc - 1 .
-
./myapp hello world→argc = 3,argv[0] = "./myapp",argv[1] = "hello",argv[2] = "world" -
./myapp -v --input=file.txt→argc = 4; no automatic parsing — you handle splitting and meaning
Basic access and safety checks
Always check argc before accessing argv[i] to avoid undefined behavior. For example, if your program expects at least one user-provided argument, verify argc > 1 .
- Use
if (argc \n"; return 1; } - Convert
argv[i]tostd::stringfor easier manipulation:std::string arg(argv[1]); - Remember:
argvelements are null-terminated C strings — safe to pass to C library functions likestrcmporatoi, but prefer C alternatives (std::stoi,std::string::compare) when possible
Simple argument parsing pattern
For basic flag-based usage (eg, -h , --help , -o output.txt ), iterate through argv starting at index 1 and inspect each string:
- Check
argv[i][0] == '-'to detect flags - Handle short options like
-v(argv[i][1] == 'v') and long options like--verboseusingstd::string(argv[i]).compare("--verbose") == 0 - If a flag expects a value (eg,
-o file.txt), ensurei 1 before reading <code>argv[i 1]
Modern alternatives (C 17 and beyond)
While argc/argv remains standard, consider wrapping them for clarity and safety:
- Create a
std::vector<:string></:string>fromargv:std::vector<:string> args(argv, argv argc);</:string> - Use libraries like Poco , argparse , or argh for robust, declarative parsing
- For simple cases, structured loops with range-based
forover a vector are more readable than raw pointer arithmetic
The above is the detailed content of How to read command-line arguments in C ? (argc, argv tutorial). For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20562
7
13665
4




