简体   繁体   English

在 C 中检查变量是字符串还是整数

[英]check if a variable is a string or an integer in C

I am writing a C program and I have to read parameters by command line.我正在编写一个 C 程序,我必须通过命令行读取参数。 How can I check if the argument passed to my program is a string (that is to say an array of characters) or an integer?如何检查传递给我的程序的参数是字符串(即字符数组)还是整数? Is there any immediate call I can use in C?我可以在 C 中使用任何即时调用吗?

You can call isdigit() on each character of the string, and if it's true for all characters you have an integer, otherwise it's some alphanumeric string.您可以对字符串的每个字符调用isdigit() ,如果所有字符都为真,则您有一个整数,否则它是一些字母数字字符串。

You can also call strtol to parse the string as an integer.您还可以调用strtol将字符串解析为整数。 The second argument returns a pointer to the first non-numeric character in the string.第二个参数返回指向字符串中第一个非数字字符的指针。 If it points to the first character, it's not an integer.如果它指向第一个字符,则它不是整数。 If it points to the end, it's an integer.如果它指向结尾,则它是一个整数。 If it points somewhere in the middle, it's an integer followed by a sequence of non-numeric characters.如果它指向中间的某个位置,则它是一个整数,后跟一系列非数字字符。

Parameters passed by command line are always strings, if you want to check if this string can be converted to integer you can use strtol :命令行传递的参数始终是字符串,如果要检查此字符串是否可以转换为整数,可以使用strtol

char *ptr = argv[1];
long num;

num = strtol(ptr, &ptr, 10);
if (*ptr == '\0')
    /* arg is a number */
else
    /* arg is NOT a number */

How can I check if the argument passed to my program is a string (that is to say an array of characters) or an integer?如何检查传递给我的程序的参数是字符串(即字符数组)还是整数?

Command line arguments are always passed to a C program as strings.命令行参数总是作为字符串传递给 C 程序。 It's up to you to figure out whether an argument represents a number or not.判断一个参数是否代表一个数字取决于你。

int is_number(char const* arg)
{
   // Add the logic to check whether arg is a number
   // The code here can be simple or complex depending on the 
   // level of checking that is necessary.
   // Should we return true or false if the argument is "1abc"?

   // This is a very simple test.
   int n;
   return (sscanf(arg, "%d", &n) == 1);
}

int main(int argc, char** argv) // argv is array of strings.
{
   int i = 0;
   for ( i = 1; i < argc; ++i )
   {
      if ( is_number(argv[i]) )
      {
         // Use the argument.
      }
   }
}

You can try to check whether all string characters fall in the range of numbers from 0-9.check this program for instance :您可以尝试检查所有字符串字符是否都在 0-9 的数字范围内。例如检查此程序:

#include <stdio.h>

int checkString(char *Str)
{
    char *ptr = Str;

    while( *ptr )
    {
        // check if string characters are within the range of numbers

        if( ! (*ptr >= 0x30 && *ptr <= 0x39 ) )
        {
            return 0;
        }
        ptr++;
    }

    return 1;
}

int main(int argc,char *argv[])
{
    // does argv[1] consist entirely of numbers ?

    if( checkString(argv[1]) )
    {
        /* if it does , do something */
        puts("success");
    }
    else
    {
        /* do something else */
    }

    return 0;
}

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

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