main(int argc, char* argv[])

main函数的两个参数名字可以修改,但是第一个参数必须是整型,表示运行程序所带的参数个数,如果一个参数也没有带,argc为1,第二个参数必须是字符型指针数组,argv[i]分别代表运行程序时所带的参数内容,argv[0]表示运行该程序的名字,和shell脚本中表示参数的$0变量类似。

例子

[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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值