在C中传递*作为shell参数时出错

AGe*_*eek 3 c

我正在编写一个小程序,我提供命令行参数

例如./a.out 2 3 4 +*

当它来'*',而不是打印'*'本身,它打印目录'+'内的文件夹工作正常.请让我知道如何删除此错误.我想在这里打印'*'.

#include <stdio.h>
int main(int argc, char *argv[])
{
   char *c;

   while(--argc > 0)
   {
      c = *++argv;
      if(strcmp(c,"+") == 0 )
      {
          printf("%s",c);
      }
      else if(strcmp(c,"-") == 0)
      {
          printf("%s",c);
      }
      else if(c[0] == '*')
      {
          printf("%s",c);
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 9

这与您的代码无关,而是与您的shell有关.如果你想要shell不是通配符通配符,那么你需要使用反斜杠或带引号来转义它们.

./foo \*
./bar '*'
Run Code Online (Sandbox Code Playgroud)