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

后端 未结 7 2255
盖世英雄少女心
盖世英雄少女心 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条回答
  •  旧时难觅i
    2020-12-18 09:35

    Let's suppose your C++ executable file is:

    /home/user/program (or C:\program.exe in Windows)

    if you execute:

    ./home/user/program 1 2 (or C:\program.exe 1 2 in Windows)

    argv[0] = /home/user/program (C:\program.exe)
    argv[1] = 1
    argv[2] = 2

    That is because:

    • argv[0] is the path of the executable file
    • argv[1] is the 1st argument

    Edit:

    Now I see that argv[0] isn't necessarily the path of the executable file.
    Read the following SO question: Is args[0] guaranteed to be the path of execution?

提交回复
热议问题