Search code examples
ccommand-line-argumentspostfix-notation

Passing * as a command line argument


I wrote a C program to evaluate reverse polish notation by passing the expression as a command line argument, but when I pass * (for multiplication) then it is passing all the file names in that folder.

For example I passed this :

./rpn 10 20 30 + *

and when I print all the arguments result is,

 10
 20
 30
 +
 gcd
 gcd.c
 gcd.c~
 rpn
 rpn.c
 rpn.c~
 swapmacro
 swapmacro.c argc :12

Solution

  • You need to escape the * e.g. by quoting it like "*" or by escaping it like \*

    The expansion of * is done by the shell (before starting your program). Read e.g. the Advanced Bash Scripting guide.