C语言中main函数的参数argc和argv
2024-08-23 04:36:13
参考资料:
C++语言中的main函数,经常带有参数argc,argv,如下:
int main(int argc, char** argv)
int main(int argc, char* argv[])
这两个参数的作用: argc 是指命令行输入参数的个数(以空白符分隔) argv存储了所有的命令行参数 假如你的程序是hello.exe,如果在命令行运行该程序,(首先应该在命令行下用 cd 命令进入到 hello.exe 文件所在目录) 运行命令为:
hello.exe Shiqi Yu
那么,argc的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。
下面的程序演示argc和argv的使用:
#include <stdio.h> int main(int argc, char ** argv)
{
int i;
for (i=0; i < argc; i++)
printf("Argument %d is %s.\n", i, argv[i]); return 0;
}
假如上述代码编译为hello.exe,那么运行
hello.exe a b c d e
将得到
Argument 0 is hello.exe.
Argument 1 is a.
Argument 2 is b.
Argument 3 is c.
Argument 4 is d.
Argument 5 is e.
运行
hello.exe lena.jpg
将得到
Argument 0 is hello.exe.
Argument 1 is lena.jpg.
最新文章
- 【大数据】Summingbird(Storm + Hadoop)的demo运行
- ios基础篇(十四)——UITableView(二)属性及基本用法
- android studio新项目时提示:Plugin is too old, please update to a more recent version
- WSB备份到远程共享文件夹的限制
- JavaScript 函数方法 - toString()
- Building Particle Filters and Particle MCMC in NIMBLE
- java之http协议
- Java作业二(2017-9-18)
- Zookeeper的作用,在Hadoop及hbase中具体作用
- Python print输出重定向到文件和屏幕,超简单
- hadoop HA (no zkfc to stop) DFSZKFailoverController进程没有启动
- beautifulSoup《转》
- angular7 + d3 显示svg
- hdu-4080 Stammering Aliens 字符串hash 模板题
- DB2序列和主键自增长
- 解决myeclipse2017安装后闪退问题
- WP runtime 获取cookie
- BZOJ4561 JLOI2016圆的异或并(扫描线+平衡树)
- Eclipse改变相同代码高亮颜色
- MongoDB(三):MongoDB概念解析
热门文章
- Java-main方法中调用非static方法
- Mac打不开inkscape怎么办
- [C#] 使 ToolTip 一直显示 (在 WinForm 与 WPF 中的差异解决方案)
- noip2019(普及组) 公交换乘 (不剪枝见祖宗题)
- [安卓基础] 008.Android中的显示单位
- ASP.NET Core 3.x API版本控制
- jchdl - GSL实例 - Assign
- Kubernetes笔记(四):详解Namespace与资源限制ResourceQuota,LimitRange
- Java实现 LeetCode 838 推多米诺(暴力模拟)
- Entity FrameWork操作数据库完成登陆、列表显示+验证码