Aja*_*jay 0 c program-entry-point
我正在使用此代码...
#include <stdio.h>
int main(int argc,char *argv[]){
int i =0;
if (argc == 1){
printf("You have entered 1 argument and you suck \n");
}
else if (argc > 1 && argc<4){
for (i=0;i<4;i++){
printf("you have entered the following args %d.%s \n",(i),argv[i]);
}
printf("\n");
}
else{
printf("You have entered more than four arguments and you suck.\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我改变从环for (i=0;i<4;i++)到for (i=0;i<7;i++),我得到如下的输出:
cam@cam:~/Desktop/D/c/c-code$ ./ex12 hi there
you have entered the following args 0../ex12
you have entered the following args 1.hi
you have entered the following args 2.there
you have entered the following args 3.(null)
you have entered the following args 4.XDG_VTNR=7
you have entered the following args 5.LC_PAPER=en_IN.UTF-8
you have entered the following args 6.ORBIT_SOCKETDIR=/tmp/orbit-cam
Run Code Online (Sandbox Code Playgroud)
为什么没有错误?为什么生成与我的操作系统相关的变量?
访问数组超出范围会导致未定义的行为.未定义行为的后果是未定义的.纯粹巧合的是,你碰巧正在阅读一些环境变量.在其他系统上,或者在这种行为的其他例子,你可能最终不约而同地读了一些其他的资料片(见心脏出血漏洞为一个显着的例子),或巧合引起的段错误(崩溃)......所有这些结果都是纯属巧合但是,不要依赖.