c++ main 函数,如果命令行参数包含 *,则 argc 值很奇怪

c++ main function, argc value is weird if command-line arguments contain *

一段非常简单的 C++ 代码,如下所示:

int main(int argc, char **argv){
    std::cout << "argc: " << argc << std::endl;
}

编译为 g++ -o hello hello.cpp

Shell 展开。 * 由 shell 扩展到当前目录中的所有文件,其中似乎有 24 个,并将它们作为单独的参数传递给您的程序。

因为这看起来像是来自 UNIX shell 的调用,请使用

./hello u \*

./hello u '*'

您需要传递 shell 在 ' ' 中解释为特殊字符的内容。

所以正确的命令行调用应该是./hello u '*'