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] 永远是程序自己的名字
相关推荐
Irissgwe24 分钟前
c++11(lambda表达式与包装器、线程库)
c++·c++11·lambda表达式·线程库·包装器·互斥量库·条件变量库
Peter·Pan爱编程1 小时前
14. Lambda 表达式:随手可写的函数对象
c++·算法·ai编程
不想写代码的星星2 小时前
从分支预测角度看 C++:为什么你的热循环慢得离谱?
c++
一抹晴空2 小时前
Keil MDK AC6 compiler编译报错,与AC5区别
c语言·arm开发·单片机
郝学胜-神的一滴2 小时前
Qt 高级开发 018:复刻经典登录界面布局与窗口美化全解析
开发语言·c++·qt·程序人生·用户界面
郝亚军2 小时前
IEEE 754 单精度浮点的SEM表示
开发语言·c++·算法
czhaii4 小时前
单片机伺服电机加减速控制子程序
c语言·单片机
Yyyyyy~4 小时前
【C++】数组篇
开发语言·c++
qq_333120974 小时前
C++高并发内存池的整体设计和实现思路_C 语言
java·c语言·c++
牛肉在哪里4 小时前
ros2 从零开始27 编写广播C++
开发语言·c++·机器人