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] 永远是程序自己的名字
相关推荐
故事和你9118 小时前
洛谷-数据结构2-1-二叉堆与树状数组1
开发语言·数据结构·c++·算法·动态规划·图论
hhb_61819 小时前
C语言核心技术难点梳理与实战案例解析
c语言·开发语言
海参崴-19 小时前
C++ STL篇 红黑树的模拟实现
开发语言·c++
研究点啥好呢19 小时前
Momenta后端开发面试题精选:10道高频考题+答案解析(数据产线方向)
c++·python·面试·求职招聘
Hical6119 小时前
C++26 前瞻心得:下一代 C++ 最值得期待的特性
c++
悲伤小伞19 小时前
Linux_传输层协议TCP详解
linux·网络·c++·网络协议·tcp/ip
笨笨饿19 小时前
#72_聊聊I2C以及他们的变体
linux·c语言·网络·stm32·单片机·算法·个人开发
Frank_refuel20 小时前
C++之STL->string类的使用和实现
java·开发语言·c++
fpcc20 小时前
跟我学C++中级篇—Linux文件读写的分析
linux·c++
南境十里·墨染春水20 小时前
linux学习进展 C语言连接mysql
linux·c语言·学习