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++)
开发语言·c++
青瓦梦滋20 小时前
协议定制/序列化-反序列化(Linux视角)
linux·服务器·网络·c++
山登绝顶我为峰 3(^v^)31 天前
C/C++ 交叉编译方法
java·c语言·c++
LONGZETECH1 天前
新能源汽车动力电池检测仿真教学系统:C/S 分层架构与数字化实训落地全解析
大数据·c语言·开发语言·人工智能·架构·系统架构·汽车
郝学胜-神的一滴1 天前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
小小龙学IT1 天前
C++ 并发编程深度解析:从内存模型到无锁数据结构
数据结构·c++
legendary_1631 天前
SINK芯片:Type-C统一供电时代的小家电核心方案
c语言·开发语言·人工智能·智能手机
_Doubletful1 天前
妙用位运算:解构汉明距离至100%(提供分析与多解)
c语言·算法·leetcode
凯瑟琳.奥古斯特1 天前
力扣1013三等分解法与C++实现
开发语言·c++·算法·leetcode·职场和发展