如何检查命令行参数是否为整数

How do I check if command line arguments are ints

我是编码和 C 的新手,我不知道如何使用命令行参数来查看它们是否为整数,如果是,请进一步使用它们。

我试过使用 sscanf,但我不确定如何使用它来检查参数是否为整数。

 #include<stdlib.h>

 #include<stdio.h>

 #define MAX_SIZE 100

int main(int argc, char* argv[]) {
   int status;
   char ch;

      status = sscanf(argv[1], "%d%c", &n, &ch);  
      if(status==1){
         printf("argument is %d", argv[1]); //to see whats arg 1

      }
      else {
    // if they're not int
          printf("Usage: Subset n k (n and k are ints)");
    }
return EXIT_SUCCESS;   
}

我想看看我是否可以打印出参数,但如果我输入一个或多个整数,它会给我一个数字,如“-432743335”。 如果我输入的不是整数,我会得到使用信息,这样就可以了 如果没有参数,那么我会得到一个分段错误:11

不要忽略警告:

foo.c:14:31: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
   14 |          printf("argument is %d", argv[1]); //to see whats arg 1
      |                              ~^   ~~~~~~~
      |                               |       |
      |                               int     char *
      |                              %s

使用 %s 将参数显示为字符串,或者使用 %d 将解析后的整数显示为整数(如此处):

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
  int status;
  int n;
  char ch;

  status = sscanf(argv[1], "%d%c", &n, &ch);
  if(status==1){
    printf("argument is %d", n);
  }
  else {
    printf("Usage: Subset n k (n and k are ints)");
  }
  return EXIT_SUCCESS;
}

为避免在不输入参数时崩溃,请检查 argc(参数数量)是否至少为 2。

你非常接近,看来你混淆了几个变量。当您尝试将 sscanf 转换为 &n 时,首先 n 未在任何地方声明,导致 未定义行为 。 (您的编译器应该向您尖叫警告和错误——上面的代码不应该编译)

不需要ch。相反,循环遍历 1 -> argc 中的所有参数(argv[0] 始终是程序名称 运行)。您在验证 sscanf 的 return 方面做得很好 -- 这是正确的编码。您可以简单地使用状态标志(任何 int 变量都可以),如果遇到非整数,只需设置您的标志 TRUE (例如,设置为零以外的任何值),这将允许您遍历所有参数,然后在最后确定是否遇到非整数,例如

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int main (int argc, char *argv[]) {

    int nonint = 0;     /* simple status flag, set if non-integer encountered */

    for (int i = 1; i < argc; i++) {            /* loop over all arguments */
        int status, n;
        status = sscanf (argv[i], "%d", &n);        /* attempt conversion */
        if (status == 1)                            /* validate return */
            printf ("argument[%d] is %d\n", i, n);  /* output int argument */
        else {  /* otherwise handle error & set nonint flag = TRUE */
            printf ("argument[%d] non-integer argument: %s.\n", i, argv[i]);
            nonint = 1;
        }
    }

    if (nonint) {   /* if nonint flag set, show usage */
        fputs ("\nUsage: Subset n k (n and k are ints)\n", stderr);
        exit (EXIT_FAILURE);
    }

    return EXIT_SUCCESS;   
}

例子Use/Output

如果有非整数参数,上面设置nonint标志,识别非整数参数后显示用法,EXIT_FAILURE是returned:

$ ./bin/cliargs 1 2 3 four 5 six 7
argument[1] is 1
argument[2] is 2
argument[3] is 3
argument[4] non-integer argument: four.
argument[5] is 5
argument[6] non-integer argument: six.
argument[7] is 7

Usage: Subset n k (n and k are ints)

验证 return:

$ echo $?
1

如果所有参数都是整数,则不会显示用法并且 EXIT_SUCCESS 是 returned:

$ ./bin/cliargs 1 2 3 4 5 6 7
argument[1] is 1
argument[2] is 2
argument[3] is 3
argument[4] is 4
argument[5] is 5
argument[6] is 6
argument[7] is 7

验证 return:

$ echo $?
0

检查一下,如果您还有其他问题,请告诉我。