Error passing * as shell argument in C

I am developing a small program where I provide command line arguments

For example,. / A.out 2 3 4 + *

When it arrives "*", instead of typing "*" itself, it prints folders inside the "+" directory works fine. Please let me know how to remove this error. I want to type '*' here.

#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);
      }
   }
}
+3
source share
2 answers

This has nothing to do with your code, but rather with your shell. If you want the shell to not hide wildcards, you will need to avoid them, either with a backslash or with quotation marks.

./foo \*
./bar '*'
+9
source

'*' . , , .

:

./a.out 2 3 4 +\*

+4

Source: https://habr.com/ru/post/1751872/


All Articles