Here is my minimal reproducible example:
#include <stdio.h>
int main( int argc, char* argv[])
{
printf (" this is the contents of argc:%d\n",argc);
int i;
for (i = 0; i < argc ; i++){
printf(" argv = %d = %s\n",i,argv[i]);
}
return 0;
}
When I change argc in the for loop into a number, lets say 10, the code crashes before it reaches 10:
$ ./argc one two three
this is the contents of argc:4
argv = 0 = ./argc
argv = 1 = one
argv = 2 = two
argv = 3 = three
argv = 4 = (null)
argv = 5 = SHELL=/bin/bash
argv = 6 = SESSION_MANAGER=local/wajih:@/tmp/.ICE-unix/1230,unix/wajih:/tmp/.ICE-unix/1230
argv = 7 = QT_ACCESSIBILITY=1
argv = 8 = COLORTERM=truecolor
argv = 9 = XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
If I for example, change argc in the for loop to a 100; I get a very long error message, which ends with this:
argv = 54 = GDMSESSION=ubuntu
argv = 55 = DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
argv = 56 = LC_NUMERIC=ar_AE.UTF-8
argv = 57 = _=./argc
argv = 58 = OLDPWD=/home/wajih
argv = 59 = (null)
Segmentation fault (core dumped).
I want to understand the reason this happens.

Most Unix System provides a 3rd argument to
mainfunction.It is called environment variables. In the above case it prints the contents of the 3rd argument -
envp. But it will not show the same behavior always. Printing data fromargvafterargccount has undefined behavior