简体   繁体   中英

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

This is not a C problem. You're using Bash (or some equivalent shell), where * is automatically expanded (before it gets anywhere near your program). You'll need to do something like this:

./rpn 10 20 30 + "*"

You need to escape the * eg by quoting it like "*" or by escaping it like \\*

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM