网络营销电子商务研究中心  
USA Web Hosting Direct
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > C/C++
User Name
Password
 
FAQ Members List Calendar 域名注册 PHP Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1   IP: 49.82.49.160
Old 2017-08-22, 05:25 PM
Sandersville Sandersville is offline
初级会员
 
Join Date: 2008-03-03
Posts: 1
Sandersville 现在声名狼藉
Default c语言中main函数参数argc,argv说明,及命令行中如何传参数

方法1.
C/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的使用:
Code:
// abiao.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#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.

Last edited by Sandersville; 2017-08-22 at 05:29 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
所有时间均为 +8. 现在的时间是 09:23 PM, Free Patent & Patent Ideas