Command line parameters c++ [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-29 18:51:59

Command line arguments are arguments passed to your program with its name. For example, the UNIX program cp (copies two files) has the following command line arguments:

cp SOURCE DEST

You can access the command line arguments with argc and argv:

int main(int argc, char *argv[])
{
    return 0;
}

argc is the number of arguments, including the program name, and argv is the array of strings containing the arguments. argv[0] is the program name, and argv[argc] is guaranteed to be a NULL pointer.

So the cp program can be implemented as such:

int main(int argc, char *argv[])
{
    char *src = argv[1];
    char *dest = argv[2];

    cpy(dest, src);
}

They do not have to be named argc and argv; they can have any name you want, though traditionally they are called that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!