search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Understanding argc and argv
Basic access and safety checks
Simple argument parsing pattern
Modern alternatives (C 17 and beyond)
Home Backend Development C++ How to read command-line arguments in C ? (argc, argv tutorial)

How to read command-line arguments in C ? (argc, argv tutorial)

Dec 30, 2025 am 04:04 AM

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.

How to read command-line arguments in C ? (argc, argv tutorial)

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 worldargc = 3 , argv[0] = "./myapp" , argv[1] = "hello" , argv[2] = "world"
  • ./myapp -v --input=file.txtargc = 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] to std::string for easier manipulation: std::string arg(argv[1]);
  • Remember: argv elements are null-terminated C strings — safe to pass to C library functions like strcmp or atoi , 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 --verbose using std::string(argv[i]).compare("--verbose") == 0
  • If a flag expects a value (eg, -o file.txt ), ensure i 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> from argv : 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 for over 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)