argc & argv

1. English Explanation

argc

  • Stands for: Argument Count
  • Meaning: The number of command-line arguments passed to the program.
  • Type: int (integer)
  • Note: argc is at least 1, because the program name itself counts as the first argument.

argv

  • Stands for: Argument Vector
  • Meaning: An array of strings that stores all command-line arguments.
  • Type: char *argv[] (or char **argv)
  • argv[0] = the program name
  • argv[1], argv[2]... = the actual arguments you input.

Simple Rule

argc = how many arguments; argv = what the arguments are.


2. 中文解释

argc

  • 全称:Argument Count
  • 含义:命令行参数的个数
  • 类型:整数 int
  • 特点:至少是 1,因为程序名本身也算一个参数。

argv

  • 全称:Argument Vector
  • 含义:存储所有命令行参数字符串数组
  • 类型:char *argv[]
  • argv[0] = 程序名
  • argv[1]argv[2]... = 你输入的实际参数

简单记忆

argc = 有几个参数;argv = 参数具体是什么。


3. Code Example (Standard main function)

c 复制代码
#include <stdio.h>

// The full main function with command line arguments
int main(int argc, char *argv[])
{
    // Print how many arguments
    printf("Total arguments (argc): %d\n", argc);

    // Print all arguments
    for (int i = 0; i < argc; i++)
    {
        printf("argv[%d] = %s\n", i, argv[i]);
    }

    return 0;
}

4. How to Run & Test

Compile:

bash 复制代码
gcc main.c -o myapp

Run with arguments:

bash 复制代码
./myapp hello 123 test

Output

复制代码
Total arguments (argc): 4
argv[0] = ./myapp
argv[1] = hello
argv[2] = 123
argv[3] = test

5. Summary Table

Name Full Name Chinese Meaning
argc Argument Count 参数个数 命令行参数的总数量(整数)
argv Argument Vector 参数向量 存储所有参数的字符串数组

最直白总结

  • argc = 你在终端输了几个词(包括程序名)
  • argv = 把你输的词存成字符串数组
  • argv[0] 永远是程序自己的名字
相关推荐
爱编码的小八嘎19 小时前
C语言完美演绎8-8
c语言
量子炒饭大师19 小时前
【C++11】RAII 义体加装指南 ——【包装器 与 异常】C++11中什么是包装器?有哪些包装器?C++常见异常有哪些?(附带完整代码讲解)
开发语言·c++·c++11·异常·包装器
达帮主19 小时前
25.C语言 递归函数
c语言·开发语言·汇编
炘爚19 小时前
深入解析内存分区:程序运行的秘密
c++
yunhuibin19 小时前
Linux 7.0 调度器:C 语言面向对象(OOPC)的极致实践
linux·运维·c语言
爱编码的小八嘎19 小时前
C语言完美演绎8-6
c语言
网域小星球19 小时前
C++ 从 0 入门(五)|C++ 面试必知:静态成员、友元、const 成员(高频考点)
开发语言·c++·面试·静态成员·友元函数
|_⊙19 小时前
C++11 右值引用
开发语言·c++
李昊哲小课19 小时前
WSL Ubuntu 24.04 GPU 加速环境完整安装指南
c++·pytorch·深度学习·ubuntu·cuda·tensorflow2
Byte不洛19 小时前
C++继承详解(菱形继承与虚拟继承)
c++·继承·面向对象·菱形继承·虚拟继承