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

后端 未结 7 2251
盖世英雄少女心
盖世英雄少女心 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:34

    By convention, argv[0] is the current program's name (or path), and argv[1] through argv[argc - 1] are the command-line arguments that the user provides.

    However, this doesn't have to be true -- programs can OS-specific functions to bypass this requirement, and this happens often enough that you should be aware of it. (I'm not sure if there's much you can do even if you're aware of it, though...)

    Example:

    gcc -O3 -o temp.o "My file.c"
    

    would (should) produce the following arguments:

    argc: 5
    argv: ["gcc", "-O3", "-o", "temp.o", "My file.c"]
    

    So saying argv[0] would refer to gcc, not to -O3.

提交回复
热议问题