使用包含圆括号的字符串作为命令行参数时出现错误


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Incorrect command line arguments.n");
    return 0;
  }
  for (int i = 0; i < strlen(argv[1]);i++){
      printf("%c",argv[1][i]);
  }
  printf("n");
}

我有这个简单的代码,但是编译后这是我的输出:

./a.out 123
123
./a.out (1+2+3)
bash: syntax error near unexpected token `1+2+3'

发生这种情况的原因是什么,我该如何修复它?似乎是括号把这个弄乱了。由于

()字符对bash有特殊含义。使用引号强制bash将它们视为普通字符:

./a.out '(1+2+3)'

最新更新