传递`*`时命令行参数号不正确

时间:2022-05-26 23:15:05

I'm writing a C program about Reverse Polish Notation, which takes its operands and operators through the command line arguments. But things go wrong when the multiply operator '*' occurs, and I don't know why.
Here is the little program to debug.

我正在编写一个关于反向波兰表示法的C程序,它通过命令行参数获取操作数和运算符。但是当乘法运算符'*'出现时出现问题,我不知道为什么。这是要调试的小程序。

test.c

test.c的

int main(int argc, char **argv)
{
    printf("%d\n", argc);
    return 0;
}

//   run case           result
    ./test a b            3
    ./test *              66

So why the '*' argument makes a wrong result ?

那么为什么'*'论证会产生错误的结果呢?

2 个解决方案

#1


11  

The * does a shell glob. So it'll expand to all the files in your current directory, which will be the arguments to your program, you have 65 files in your directory. You can see what's happening if you run echo *

*做一个shell glob。因此,它将扩展到当前目录中的所有文件,这将是程序的参数,您的目录中有65个文件。你可以看看如果你运行echo *会发生什么

You need to single quote the * as in ./test '*' (double quotes would work too) , this will prevent the shell expanding *. A * is given to your program in this case, the shell removes the single quotes.

你需要单引号*,如./test'*'(双引号也可以),这将阻止shell扩展*。在这种情况下,程序会给你一个*,shell会删除单引号。

If you want to evaluate expressions, you could do

如果你想评估表达式,你可以这样做

./test 3 2 '*'

In this case your program receives 3 additional arguments separated so argv[1] is 3, argv[2] is 2 and argv[3] is *

在这种情况下,你的程序接收3个额外的参数,因此argv [1]为3,argv [2]为2,argv [3]为*

Or you could do:

或者你可以这样做:

./test '3 2 *'

In this case, your program receives 1 additional argument, argv[1] will be the string 3 2 *

在这种情况下,你的程序会收到1个额外的参数,argv [1]将是字符串3 2 *

#2


8  

Your command shell is treating * as a wildcard. It's probably including every file in the current directory: 60ish in your case.

您的命令shell将*视为通配符。它可能包括当前目录中的每个文件:在你的情况下为60ish。

#1


11  

The * does a shell glob. So it'll expand to all the files in your current directory, which will be the arguments to your program, you have 65 files in your directory. You can see what's happening if you run echo *

*做一个shell glob。因此,它将扩展到当前目录中的所有文件,这将是程序的参数,您的目录中有65个文件。你可以看看如果你运行echo *会发生什么

You need to single quote the * as in ./test '*' (double quotes would work too) , this will prevent the shell expanding *. A * is given to your program in this case, the shell removes the single quotes.

你需要单引号*,如./test'*'(双引号也可以),这将阻止shell扩展*。在这种情况下,程序会给你一个*,shell会删除单引号。

If you want to evaluate expressions, you could do

如果你想评估表达式,你可以这样做

./test 3 2 '*'

In this case your program receives 3 additional arguments separated so argv[1] is 3, argv[2] is 2 and argv[3] is *

在这种情况下,你的程序接收3个额外的参数,因此argv [1]为3,argv [2]为2,argv [3]为*

Or you could do:

或者你可以这样做:

./test '3 2 *'

In this case, your program receives 1 additional argument, argv[1] will be the string 3 2 *

在这种情况下,你的程序会收到1个额外的参数,argv [1]将是字符串3 2 *

#2


8  

Your command shell is treating * as a wildcard. It's probably including every file in the current directory: 60ish in your case.

您的命令shell将*视为通配符。它可能包括当前目录中的每个文件:在你的情况下为60ish。