how to check if the input is a number or not in C?

前端 未结 8 2093
暖寄归人
暖寄归人 2020-11-27 05:38

In the main function of C:

void main(int argc, char **argv)
{
   // do something here
}

In the command line, we will type any number for ex

8条回答
  •  醉话见心
    2020-11-27 06:09

    A self-made solution:

    bool isNumeric(const char *str) 
    {
        while(*str != '\0')
        {
            if(*str < '0' || *str > '9')
                return false;
            str++;
        }
        return true;
    }
    

    Note that this solution should not be used in production-code, because it has severe limitations. But I like it for understanding C-Strings and ASCII.

提交回复
热议问题