c - C如果条件不接受参数

标签 c

我是C语言的新手,正在尝试使用if语句来检查参数(在本例中为“jobs”),但它似乎没有用...

int builtin_cmd(char **argv)
{
  printf("test1\n");
  if (!strcmp(argv[0], "quit")) { //quit command
    exit(0);
  }
  if ((!strcmp(argv[0], "fg")) || (!strcmp(argv[0], "bg"))) { //fg or bg command
    do_bgfg(argv);
    return 1;
  }
  if (!strcmp(argv[0], "jobs")) { //jobs command
    printf("test2\n");
    listjobs(jobs);
    printf("test3\n");
    return 1;
  }
  printf("test4\n");
  return 0;     /* not a builtin command */
}

我输入了“作业”,但是基于测试输出(1-4重复),它没有注册。有谁知道可能出什么问题了?

最佳答案

argv[0]指向程序名称,而不是第一个参数。如果存在该参数,则argv[1]指向该位置。

C标准,§ 5.1.2.2.1, Program Startup:

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.



强调我的。

关于c - C如果条件不接受参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680580/

相关文章:

c - 何时对数组使用指针与使用访问运算符的通用规则是什么?

c - Linux : Signal : SIGUSR1 : Signal Handler : Measuring execution time?

c - 通过函数传递变量

c++ - 使用 C/C++ 编写隐写术应用程序

更改 char 数组的值

c++ - 多重指数实现

c - 在 epoll 的事件结构中使用 void ptr

c - fastText 和 word2vec : NaNs in accuracy computation code

c - 服务器无法正确读取/打开客户端用 C 发送的文件名

c - 绑定(bind)套接字时为什么要将端口号转换为网络字节序?