main(int argc, char* argv[])
2014-10-27 21:48
337 查看
main函数的两个参数名字可以修改,但是第一个参数必须是整型,表示运行程序所带的参数个数,如果一个参数也没有带,argc为1,第二个参数必须是字符型指针数组,argv[i]分别代表运行程序时所带的参数内容,argv[0]表示运行该程序的名字,和shell脚本中表示参数的$0变量类似。
例子:
编译&运行:
main()函数还有另外一种格式:main( int argc, char* argv[], char* envp[]), 第三个参数也同样为字符型指针数组,为当前的环境变量的内容
例子:
[test@rhel6164 c]$ cat test.c #include <stdio.h> #include <stdlib.h> //ANSI C exit() int main(int argc, char* argv[]) { if ( 1 == argc ) { printf("This is no parameter for this program. "); printf("The argc is %d.\n", argc); exit(1); } else { printf("The number of parameters is %d.\n", argc - 1); printf("The argc is %d.\n", argc); for (int i = 0; i < argc; i++) { printf("The argv[%d] is %s\n", i, argv[i]); } } return 0; }
编译&运行:
[test@rhel6164 c]$ gcc -std=c99 test.c [test@rhel6164 c]$ ./a.out testing main The number of parameters is 2. The argc is 3. The argv[0] is ./a.out //运行时改程序的名字 The argv[1] is testing //第一个参数字符串 The argv[2] is main //第二个参数字符串
main()函数还有另外一种格式:main( int argc, char* argv[], char* envp[]), 第三个参数也同样为字符型指针数组,为当前的环境变量的内容
[test@rhel6164 c]$ cat test.c #include <stdio.h> int main(int argc, char* argv[], char* envp[]) { for (int i = 0; *envp[i] != '\0'; i++) { printf("The envp[%d] is %s\n", i, envp[i]); //依次打印envp[i]的内容 } return 0; }编译&运行:
[test@rhel6164 c]$ gcc -std=c99 ./test.c [test@rhel6164 c]$ ./a.out The envp[0] is HOSTNAME=rhel6164.localdomain The envp[1] is SHELL=/bin/bash The envp[2] is TERM=xterm The envp[3] is HISTSIZE=1000 The envp[4] is USER=test The envp[5] is LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36: The envp[6] is MAIL=/var/spool/mail/test The envp[7] is PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/test/bin The envp[8] is PWD=/home/test/c The envp[9] is LANG=en_US.UTF-8 The envp[10] is KDE_IS_PRELINKED=1 The envp[11] is KDEDIRS=/usr The envp[12] is SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass The envp[13] is HISTCONTROL=ignoredups The envp[14] is SHLVL=1 The envp[15] is HOME=/home/test The envp[16] is LOGNAME=test The envp[17] is CVS_RSH=ssh The envp[18] is LESSOPEN=|/usr/bin/lesspipe.sh %s The envp[19] is G_BROKEN_FILENAMES=1 The envp[20] is _=./a.out The envp[21] is OLDPWD=/home/test
相关文章推荐
- main(int argc , char *argv[])
- 调试状态下向main(int argc, char **argv)传参数
- main(int argc,char **argv)参数解析
- linux c之main(int argc, char *argv[], char *envp[])参数意义
- main( int argc, char *argv[]) 理解main函数参数含义
- 主函数main中变量(int argc,char *argv[])的含义
- int main(int argc, char* argv[])和int _tmain(int argc, _TCHAR* argv[])区别
- main()与main(int argc,char *argv[])
- FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
- 简单的main(int argc, char* argv[])
- main(int argc,char *argv[] )参数输入
- main(int argc, char ** argv)形参的理解与使用
- main(int argc,char* argv[],char *envp[] )
- Linux上程序执行的入口--main(int argc, char *argv[])
- main(int argc,char *argv[ ],char *env)参数详解
- 主函数main中变量( int argc,char **argv )的含义
- main (int argc,char *argv[]) 解析
- main(int argc, char **argv)参数argc,argv说明和指向指针的指针变量
- C语言中main( int argc, char *argv[] )和getopt()函数学习
- 如何在vs2010中设置C++ main 函数的实参int main(int argc ,char *argv[])