void main(int argc,char *argv[])
{
for (int i = 0; i < argc; i++)
{
printf("%s ", argv[i]);
}
}
when I use command ./test 1 2 3 in terminal to execute this program, I got result ./test 1 2 3 ,but when I use function execl("/usr/src/test", "1", "2", "3", NULL) in another program I got result
1 2 3,why?
The syntax of
execl()is:So you have
The
argNparameters are put into theargvarray of the new process.You have to repeat the path as
arg0to put that intoargv[0].This isn't done automatically because
argv[0]isn't required to be the same as the program path, and there are some situations where it isn't (for instance, login shells are invoked by adding a-prefix inargv[0]).