main函数参数的使用方法

学习自:https://zhuanlan.zhihu.com/p/692325239

 

#include "stdio.h"
int main(int argc, char const *argv[])
{
    // 遍历用户调用程序时输入的参数
    // leetcode/main_arg.c
    // gcc main_arg.c -o my_executable_file
    // .\my_executable_file.exe 1 2 3 6
    int target=argv[4][0];
    for (char i = 0; i < argc; i++)
    {
        printf("\e[1;47;34m[%s %d]: %s\e[0m\n",__FUNCTION__,__LINE__,argv[i]);
    }
    return 0;
}

 //编译指令:gcc main_arg.c -o my_executable_file

 //程序调用指令:.\my_executable_file.exe 1 2 3 6

/*

运行结果:

[main 11]: D:\c_projects\VSCode_project\语法\my_executable_file.exe
[main 11]: 1
[main 11]: 2
[main 11]: 3
[main 11]: 6

 */

//注意:字符与数值之间隔着ASCII码

 编译指令:gcc main_arg.c -o my_executable_file

   程序调用指令:.\my_executable_file.exe 1 2 3 6

运行结果截图:

 

posted @ 2025-07-13 09:48  败人两字非傲即惰  阅读(8)  评论(0)    收藏  举报