MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   C/C++ (https://www.labfans.com/bbs/forumdisplay.php?f=70)
-   -   [资料] C语言命令行参数 (https://www.labfans.com/bbs/showthread.php?t=13410)

langqi 2011-05-16 12:58

C语言命令行参数
 
int main (int argc,char*argv[])的使用方法和使用心得。
c语言中命令行参数argc,argv (1)

2009-08-12 11:36:02 [url]www.hackbase.com[/url] 来源:互联网
c语言中命令行参数argc,argv main(int argc,char **argv) argv为指针的指针 argc为整数 char **argv or: char *argv[] or: char argv[][] main()括号内是固定的写法。 下面给出一个例子来理解这两 ...
c语言中命令行参数argc,argv
main(int argc,char **argv)
argv为指针的指针
argc为整数
char **argv or: char *argv[] or: char argv[][]
main()括号内是固定的写法。
下面给出一个例子来理解这两个参数的用法:
假设程序的名称为prog,
当只输入prog,则由操作系统传来的参数为:
argc=1,表示只有一程序名称。
argc只有一个元素,argv[0]指向输入的程序路径及名称:./prog
当输入prog para_1,有一个参数,则由操作系统传来的参数为:
argc=2,表示除了程序名外还有一个参数。
argv[0]指向输入的程序路径及名称。
argv[1]指向参数para_1字符串。
当输入prog para_1 para_2 有2个参数,则由操作系统传来的参数为:
argc=3,表示除了程序名外还有2个参数。
argv[0]指向输入的程序路径及名称。
argv[1]指向参数para_1字符串。
argv[2]指向参数para_2字符串。
void main( int argc, char *argv[] )
char *argv[] : argv 是一个指针数组,他的元素个数是argc,存放的是指向每一个参数的指针,
他的第一个元素即argv[0]为编译生成的可执行文件名(包括路径 eg:"F:\VC\Ex1\Debug\Ex1.exe"),
从二个元素(argv[1])开始,
是每一个参数 int argc 表示argv的大小,是实际参数个数+1,
其中+1是因为argv[0]是编译后的可执行文件名
main()主函数
每一C 程序都必须有一main()函数, 可以根据自己的爱好把它放在程序的某
个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放
在哪个地方, 以下几点说明都是适合的。
1. main() 参数
在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。
* argc: 整数, 为传给main()的命令行参数个数。
* argv: 字符串数组。
在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0
以下的版本, argv[0]为空串("") 。
argv[1] 为在DOS命令行中执行程序名后的第一个字符串;
argv[2] 为执行程序名后的第二个字符串;
...
argv[argc]为NULL。
*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符
串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C:
\TURBOC(对于PATH) 或YES(对于87)。
Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中
说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序
的局部变量。
请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下
的例子:
main()
main(int argc)
main(int argc, char *argv[])
main(int argc, char *argv[], char *env[])
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不
用argv[]的情况。
以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数:
/*program name EXAMPLE.EXE*/
#i nclude <stdio.h>
#i nclude <stdlib.h>
main(int argc, char *argv[], char *env[])
{
int i;
printf("These are the %d command- line arguments passed to
main:\n\n", argc);
for(i=0; i<=argc; i++)
printf("argv[%d]:%s\n", i, argv[i]);
printf("\nThe environment string(s)on this system are:\n\n");
for(i=0; env[i]!=NULL; i++)
printf(" env[%d]:%s\n", i, env[i]);
}
如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE:
C:\example first_argument "argument with blanks" 3 4 "last but
one" stop!
注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument
with blanks"和"Last but one")。
结果是这样的:
The value of argc is 7
These are the 7 command-linearguments passed to main:
argv[0]:C:\TURBO\EXAMPLE.EXE
argv[1]:first_argument
argv[2]:argument with blanks
argv[3]:3
argv[4]:4
argv[5]:last but one
argv[6]:stop!
&nbsp; argv[7]:(NULL)
The environment string(s) on this system are:
env[0]: COMSPEC=C:\COMMAND.COM
env[1]: PROMPT=$P$G /*视具体设置而定*/
env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/

应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包
括参数间的空格), 这是由DOS 限制的。

wangbian 2011-11-06 00:32

回复: C语言命令行参数
 
牛人,我搞的是案桌的研发

chirtmask8s 2012-07-19 00:22

回复: C语言命令行参数
 
[QUOTE=;][/QUOTE]
Altec computer speakers can support lots of brands of PC or laptop Coming in the size of 42cm x 38cm x 16cm,[url=http://www.louisvuittonoutletjuly.com]louis vuitton[/url], it has one exterior patch pocket at front and one exterior zip pocket at back greatly spans its capacity Ask for a detailed breakdown on the parts replaced when you get a bill or invoice5
The hopes that all of us will help and support UNICEF efforts in the fight against poverty in the coming years This is why you should replace the string of your pearl necklaces once in two or three yearsI was shopping yesterday and counted half a dozen other women (of all ages) wearing Christmas vests,[url=http://www.uklouisvuittonmart.com]http://www.uklouisvuittonmart.com[/url], sweaters and tees so I know I'm not alone It is truly a blessing, since you can buy right from your house and your free time
This is very important for us to identify What if you were truly a shopaholic and had the slightest impulse to buy anything you felt you 搉eeded? You didn抰 have the money yet it was dire and almost imperative that you have that dress that just may go to the clearance rack in about two weeks or that wallet that you swear is cheaper at this particular store According to a poll by Gallup international, almost half of young unmarried Americans between the ages of 20 to 29 are ready to have marriage abolished Throughout the holiday, it's a wise decision to go shopping to alleviate the mood, when you glance at the colorful things, maybe your own mood can be better,[url=http://www.louisvuittonoutletfavour.com]louis vuitton handbags uk[/url], just forget about each of the displeasures for a while, right after when you purchase a [url=http://www.louisvuittonoutletfavour.com]Louis Vuitton[/url] Multicolor handbag, its colorful aspect may possibly improve your feelings, Courtney MM is stuffed with fashionable design and chromatic color, it will definitely play a significant part to make you really feel different with all the normal nights
Now we are more in love than ever Serving size: ?cup This type is usually soft or semi-soft material suitcase He took up most of the room
Related articles:


[url=http://www.antikriegsgruppe.de/forum/viewtopic.php?p=153242#153242]louis vuitton handbags2346beautiful[/url]


所有时间均为北京时间。现在的时间是 23:13

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.