在 C 中处理命令行参数时出错
Error in handling command line arguments in C
我正在尝试编写一个 C 代码,它将以 {num1} {operator} {num2}
的形式从命令行获取三个参数。对于 *
.
以外的所有运算符符号,它工作正常
这是验证这一点的代码:
#include <stdio.h>
int main(int argc, char const *argv[]) {
printf("%d\n", argc);
}
我将其编译为 gcc test.c -o test
。
然后当我 运行 它使用 ./test 5 + 9
时,它给出了预期的输出 4
。但是当我 运行 ./test 5 * 9
时,它给出 25
作为输出。我在这里做错了什么?
*
被 shell 扩展为当前目录中所有文件的列表。如果你不想 shell-expansion 你需要(单)引用参数:
./test 5 '*' 9
我正在尝试编写一个 C 代码,它将以 {num1} {operator} {num2}
的形式从命令行获取三个参数。对于 *
.
这是验证这一点的代码:
#include <stdio.h>
int main(int argc, char const *argv[]) {
printf("%d\n", argc);
}
我将其编译为 gcc test.c -o test
。
然后当我 运行 它使用 ./test 5 + 9
时,它给出了预期的输出 4
。但是当我 运行 ./test 5 * 9
时,它给出 25
作为输出。我在这里做错了什么?
*
被 shell 扩展为当前目录中所有文件的列表。如果你不想 shell-expansion 你需要(单)引用参数:
./test 5 '*' 9