Main函数中参数argc,argv说明
2015-06-03 11:46
561 查看
转载自:/article/1422835.html
C/C++语言中的main函数,经常带有参数argc,argv,如下:
[cpp] view
plaincopy
int main(int argc, char** argv)
这两个参数的作用是什么呢?argc 是指命令行输入参数的个数,argv存储了所有的命令行参数。假如你的程序是hello.exe,如果在命令行运行该程序,(首先应该在命令行下用 cd 命令进入到 hello.exe 文件所在目录) 运行命令为:
[cpp] view
plaincopy
hello.exe Shiqi Yu
那么,argc的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。
下面的程序演示argc和argv的使用:
[cpp] view
plaincopy
#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,那么运行:
[cpp] view
plaincopy
hello.exe a b c d e
将得到
[cpp] view
plaincopy
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.
运行:
[cpp] view
plaincopy
hello.exe lena.jpg
将得到
[cpp] view
plaincopy
Argument 0 is hello.exe.
Argument 1 is lena.jpg.
C/C++语言中的main函数,经常带有参数argc,argv,如下:
[cpp] view
plaincopy
int main(int argc, char** argv)
这两个参数的作用是什么呢?argc 是指命令行输入参数的个数,argv存储了所有的命令行参数。假如你的程序是hello.exe,如果在命令行运行该程序,(首先应该在命令行下用 cd 命令进入到 hello.exe 文件所在目录) 运行命令为:
[cpp] view
plaincopy
hello.exe Shiqi Yu
那么,argc的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。
下面的程序演示argc和argv的使用:
[cpp] view
plaincopy
#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,那么运行:
[cpp] view
plaincopy
hello.exe a b c d e
将得到
[cpp] view
plaincopy
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.
运行:
[cpp] view
plaincopy
hello.exe lena.jpg
将得到
[cpp] view
plaincopy
Argument 0 is hello.exe.
Argument 1 is lena.jpg.
相关文章推荐
- MFC单文档工程的使用小结(CDocument, CView, CMainFrame)
- LeetCode: Contains Duplicate
- Onpaint()函数中绘图出现问题:当多次进入onpaint()发现次数达到一定程度就会出现窗口不能再重绘导致窗口内容损坏的现象
- AIDL机制实现进程间的通讯实例
- storyboard plain style unsupported in a navigation item
- document.domain 跨域问题
- [Leetcode 217, Easy] Contains Duplicate
- [Leetcode 219, Easy] Contains Duplicate II
- sendMail 发邮件 tls
- 【安卓学习之路1】轻轻一点,好神奇——给activity_main.xml添加Button控件
- URAL 1152 Faise Mirrors 状压DP 简单题
- Container With Most Water
- Motion Hosts Wheelchair
- videos and multimediaIn
- 安装问题Error: dl failure on line 864
- [LeetCode] Contains Duplicate II
- hdu1848 Fibonacci again and again(SG函数博弈)
- Mac系统安装Rails
- #219 Contains Duplicate II
- [leetcode] Contains Duplicate III