繁体   English   中英

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

[英]How do I check if command line arguments are ints

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

我曾尝试使用 sscanf 但我不确定如何使用它来检查 arguments 是否为整数。

 #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;   
}

我想看看我是否可以打印出 arguments 但如果我输入一个或多个整数,它会给我一个像“-432743335”这样的数字。 如果我输入 integer 以外的其他内容,我会得到使用信息,以便正常工作如果没有 arguments,那么我会收到分段错误: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的字符串,要么将已解析的 integer 显示为带有%d的 integer (如此处):

#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循环遍历所有 arguments ( argv[0]始终是正在运行的程序名称)。 您在验证sscanf的返回方面做得很好——这是正确的编码。 您可以简单地使用状态标志(任何int变量都可以),如果遇到非整数,只需将标志设置为TRUE (例如,设置为零以外的任何值),这将允许您遍历所有 arguments 然后确定最后,如果遇到非整数,例如

#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;   
}

示例使用/输出

如果有非整数arguments,上面设置了nonint标志,识别出非整数arguments后显示用法并返回EXIT_FAILURE

$ ./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)

验证返回:

$ echo $?
1

如果所有 arguments 都是整数,则不显示用法并返回EXIT_SUCCESS

$ ./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

验证返回:

$ echo $?
0

如果您还有其他问题,请仔细查看并告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM