1、问题
我们常见的int main(int argc, char *argv[], char *envp[]) 各个参数的意义。
2、代码
#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[], char *envp[])
{
printf("argc ###\n%d\n",argc);
puts("argc end *****************************");
while (*argv) {
printf("%s\n", *(argv++));
}
puts("argv end ~***************************");
while (*envp) {
printf("%s\n", *(envp++));
}
return 0;
}
3、结果展示
chenyu@chenyu:~/Desktop/linux$ gcc -g argv.c -o argv
chenyu@chenyu:~/Desktop/linux$ ./argv
argc ###
1
argc end *****************************
./argv
argv end ~***************************
XDG_VTNR=7
LC_PAPER=en_US.UTF-8
NDK=/home/chenyu/Android_dev/android-ndk-r10e
LC_ADDRESS=en_US.UTF-8
XDG_SESSION_ID=c2
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/chenyu
LC_MONETARY=en_US.UTF-8
CLUTTER_IM_MODULE=xim
GPG_AGENT_INFO=/home/chenyu/.gnupg/S.gpg-agent:0:1
TERM=xterm-256color
VTE_VERSION=4205
SHELL=/bin/bash
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
WINDOWID=90190046
LC_NUMERIC=en_US.UTF-8
UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1406
GNOME_KEYRING_CONTROL=
GTK_MODULES=gail:atk-bridge:unity-gtk-module
NO_PROXY=localhost,127.0.0.0/8,::1
http_proxy=http://199.200.72.4:8080/
USER=chenyu
QT_ACCESSIBILITY=1
LC_TELEPHONE=en_US.UTF-8
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
ftp_proxy=ftp://199.200.72.4:8080/
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg
all_proxy=socks://199.200.72.4:8080/
ALL_PROXY=socks://199.200.72.4:8080/
DESKTOP_SESSION=ubuntu
PATH=/home/chenyu/Android_dev/sdk/tools:/home/chenyu/Android_dev/sdk/platform-tools:/home/chenyu/Android_dev/sdk/build-tools/23.0.2:/home/chenyu/Android_dev/android-ndk-r10e:/home/chenyu/Android_dev/jdk1.8.0_11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
QT_IM_MODULE=fcitx
QT_QPA_PLATFORMTHEME=appmenu-qt5
LC_IDENTIFICATION=en_US.UTF-8
XDG_SESSION_TYPE=x11
PWD=/home/chenyu/Desktop/linux
JOB=gnome-session
socks_proxy=socks://199.200.72.4:8080/
XMODIFIERS=@im=fcitx
JAVA_HOME=/home/chenyu/Android_dev/jdk1.8.0_11
GNOME_KEYRING_PID=
LANG=en_US.UTF-8
GDM_LANG=en_US
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
LC_MEASUREMENT=en_US.UTF-8
COMPIZ_CONFIG_PROFILE=ubuntu
IM_CONFIG_PHASE=1
PAPERSIZE=letter
https_proxy=https://199.200.72.4:8080/
GDMSESSION=ubuntu
SESSIONTYPE=gnome-session
GTK2_MODULES=overlay-scrollbar
SHLVL=1
HOME=/home/chenyu
XDG_SEAT=seat0
LANGUAGE=en_US:en
no_proxy=localhost,127.0.0.0/8,::1
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
UPSTART_INSTANCE=
UPSTART_EVENTS=started starting
XDG_SESSION_DESKTOP=ubuntu
LOGNAME=chenyu
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
CLASSPATH=.:/home/chenyu/Android_dev/jdk1.8.0_11/lib:/home/chenyu/Android_dev/jdk1.8.0_11/jre/lib
QT4_IM_MODULE=fcitx
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-l8HM2dsSoK
LESSOPEN=| /usr/bin/lesspipe %s
INSTANCE=Unity
UPSTART_JOB=unity-settings-daemon
XDG_RUNTIME_DIR=/run/user/1000
DISPLAY=:0
XDG_CURRENT_DESKTOP=Unity
GTK_IM_MODULE=fcitx
LESSCLOSE=/usr/bin/lesspipe %s %s
LC_TIME=en_US.UTF-8
LC_NAME=en_US.UTF-8
XAUTHORITY=/home/chenyu/.Xauthority
OLDPWD=/home/chenyu/Desktop
_=./argv
4、总结:
argc是命令行参数个数,char *argv[]是指所有命令行参数,char *envp[]是环境变量。