原文:http://hi.baidu.com/luosiyong/blog/item/eab58efbb7223f254f4aeaef.html
控制台应用程序的main函数4种写法:
main()
main(int argc)
main(int argc,char * argv[])
main(int argc,char * argv[],char * env[])
即里面的参数argc,argv,env都是可选的,但是顺序必须是argc,argv,env。
其中:
argc:参数的个数,这个在程序运行时输入的命令行中字符串的个数决定。
argv:具体的参数,每个参数是一个字符数组指针。
env:环境变量参数,由系统的当前环境变量的设置决定。
具体的代码示例:
//r.cpp
#include <stdio.h>
void main(int argc)
{
printf("argc=%d\n",argc);
}
运行结果:
D:\CORC++\Debug>r
argc=1
D:\CORC++\Debug>r a
argc=2
***************************************************************
//s.cpp
#include <stdio.h>
void main(int argc,char * argv[])
{
printf("argc=%d\n",argc);
printf("command-line name : %s\n",argv[0]);
for(int i=1;i<argc;i++)
printf("Param[%d] : %s\n",i,argv[i]);
}
运行结果:
D:\CORC++\Debug>s
argc=1
command-line name : s
D:\CORC++\Debug>s a b c
argc=4
command-line name : s
Param[1] : a
Param[2] : b
Param[3] : c
**************************************************************
//t.cpp
#include <stdio.h>
void main(int argc,char * argv[],char * env[])
{
int i;
printf("argc=%d\n",argc);
for(i=0;i<=argc;i++)
printf("argv[%d] : %s\n",i,argv[i]);
printf("The environment sring(s) on this system are : \n");
for(i=0;* env!=NULL;i++)
printf("env[%d] : %s\n",i,* env++);
}
运行结果:
D:\downloads\CORC++\Debug>t
argc=1
argv[0] : t
argv[1] : (null)
The environment sring(s) on this system are :
env[0] : ALLUSERSPROFILE=C:\Documents and Settings\All Users
env[1] : APPDATA=C:\Documents and Settings\founder\Application Data
env[2] : CLASS_HOME=D:\Program Files\Java\jdk1.6.0_02\lib\dt.jar;.;D:\Program Files\Java\jdk1.6.0_02\lib
env[3] : CommonProgramFiles=C:\Program Files\Common Files
env[4] : COMPUTERNAME=LUOSIYONG
env[5] : ComSpec=C:\WINDOWS\system32\cmd.exe
env[6] : FP_NO_HOST_CHECK=NO
env[7] : HOMEDRIVE=C:
env[8] : HOMEPATH=\Documents and Settings\founder
env[9] : JAVA_HOME=D:\Program Files\Java\jdk1.6.0_02
env[10] : LOGONSERVER=\\LUOSIYONG
env[11] : NUMBER_OF_PROCESSORS=1
env[12] : OS=Windows_NT
env[13] : Path=C:\windows\system32;D:\Program Files\Java\jdk1.6.0_02\bin;D:\xampp\mysql\bin
env[14] : PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
env[15] : PROCESSOR_ARCHITECTURE=x86
env[16] : PROCESSOR_IDENTIFIER=x86 Family 15 Model 47 Stepping 2, AuthenticAMD
env[17] : PROCESSOR_LEVEL=15
env[18] : PROCESSOR_REVISION=2f02
env[19] : ProgramFiles=C:\Program Files
env[20] : PROMPT=$P$G
env[21] : SESSIONNAME=Console
env[22] : SystemDrive=C:
env[23] : SystemRoot=C:\WINDOWS
env[24] : TEMP=C:\DOCUME~1\founder\LOCALS~1\Temp
env[25] : TMP=C:\DOCUME~1\founder\LOCALS~1\Temp
env[26] : USERDOMAIN=LUOSIYONG
env[27] : USERNAME=founder
env[28] : USERPROFILE=C:\Documents and Settings\founder
env[29] : VS90COMNTOOLS=D:\Program Files\VS2008\Common7\Tools\
env[30] : windir=C:\WINDOWS
D:\CORC++\Debug>t luosiyong lucky dog
argc=4
argv[0] : t
argv[1] : luosiyong
argv[2] : lucky
argv[3] : dog
argv[4] : (null)
The environment sring(s) on this system are :
env[0] : ALLUSERSPROFILE=C:\Documents and Settings\All Users
env[1] : APPDATA=C:\Documents and Settings\founder\Application Data
env[2] : CLASS_HOME=D:\Program Files\Java\jdk1.6.0_02\lib\dt.jar;.;D:\Program Files\Java\jdk1.6.0_02\lib
env[3] : CommonProgramFiles=C:\Program Files\Common Files
env[4] : COMPUTERNAME=LUOSIYONG
env[5] : ComSpec=C:\WINDOWS\system32\cmd.exe
env[6] : FP_NO_HOST_CHECK=NO
env[7] : HOMEDRIVE=C:
env[8] : HOMEPATH=\Documents and Settings\founder
env[9] : JAVA_HOME=D:\Program Files\Java\jdk1.6.0_02
env[10] : LOGONSERVER=\\LUOSIYONG
env[11] : NUMBER_OF_PROCESSORS=1
env[12] : OS=Windows_NT
env[13] : Path=C:\windows\system32;D:\Program Files\Java\jdk1.6.0_02\bin;D:\xampp\mysql\bin
env[14] : PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
env[15] : PROCESSOR_ARCHITECTURE=x86
env[16] : PROCESSOR_IDENTIFIER=x86 Family 15 Model 47 Stepping 2, AuthenticAMD
env[17] : PROCESSOR_LEVEL=15
env[18] : PROCESSOR_REVISION=2f02
env[19] : ProgramFiles=C:\Program Files
env[20] : PROMPT=$P$G
env[21] : SESSIONNAME=Console
env[22] : SystemDrive=C:
env[23] : SystemRoot=C:\WINDOWS
env[24] : TEMP=C:\DOCUME~1\founder\LOCALS~1\Temp
env[25] : TMP=C:\DOCUME~1\founder\LOCALS~1\Temp
env[26] : USERDOMAIN=LUOSIYONG
env[27] : USERNAME=founder
env[28] : USERPROFILE=C:\Documents and Settings\founder
env[29] : VS90COMNTOOLS=D:\Program Files\VS2008\Common7\Tools\
env[30] : windir=C:\WINDOWS
【注解】其中运行结果均是在命令行调用生成的exe的运行结果。