c - 使用 "*"作为函数 main() 的参数

标签 c shell arguments program-entry-point

当我想使用 * 作为函数 main() 的参数时,shell 会将其展开为当前目录中的所有文件。如何避免这种情况?

int main(int argc, char *argv[])
{
    printf("%d\n", argc);
    for(int i=0; i<argc-1; i++)
    {
        printf("%s \n", argv[i]);
    }
    printf("\n");
}

在命令行中,它会输出:

atlas@localhost ~/D/P/C> ./expr 2 3 4 + *
13
./expr 
2 
3 
4 
+ 
Command.c 
Readlines.c 
catlas.h 
expr 
expr.c 
find 
find.c 

最佳答案

* 是在 shell 上下文中使用的特殊通配符。在 实际 传递给您的程序之前,shell 总是会展开 *。要将 * 的输入作为命令行参数字符,您可以将 * 括在引号中,例如 "*" 或使用转义符字符 \*,如其他答案中所建议的那样,停止扩展。

否则,* 的扩展在 传递给您的程序之前由 shell 执行。

关于c - 使用 "*"作为函数 main() 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578605/

相关文章:

c - 什么是 char * const *?

c - C中typedef结构中的函数指针

C 项目 - 两个库对不同类型使用相同的 typedef 标识符

Linux top -b 只显示特定列

R 系统函数总是返回错误 127

javascript - 如何访问实例化为参数的对象?

c - scanf 多个变量

c - 如何捕获 SIGINT 并让它只杀死 C 中的前台进程?

php - 传递给 myFunction() 的参数 1 必须是 string 的实例,string given, called in

python - 需要使用 argparse 传递带单引号的字符串参数