int main(int argc, char *argv[])

后端 未结 7 2257
盖世英雄少女心
盖世英雄少女心 2020-12-18 08:52

If I have this:

int main(int argc, char *argv[])

In the body, you can sometimes find programs using argv[1].

When do w

7条回答
  •  长情又很酷
    2020-12-18 09:42

    Yes, that's mostly it, argv[1] is the second command line parameter. The first command line parameter is the name of the program itself.

    Alternatively, to avoid the semantic mess that this answer originally had, and the comments from others, it might make sense to call argv[0] the zeroth parameter, so that argv[1] would now be the "first" of the user supplied values.

    In any event, this comes from the exec() family of functions, e.g. execl which has usage:

     int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
    

    In the (Unix) shell when you type in a command, if necessary the shell first resolves the command name (using $PATH) to find the real absolute path. The (absolute or relative) path is supplied for path, and the command as originally typed-in is supplied as arg0, eventually becoming argv[0] in your program.

    The remaining command line parameters then end up as argv[1], etc.

提交回复
热议问题