Char Comparison in C

前端 未结 3 512
野性不改
野性不改 2021-01-01 12:56

I\'m trying to compare two chars to see if one is greater than the other. To see if they were equal, I used strcmp. Is there anything similar to strcmp

3条回答
  •  暖寄归人
    2021-01-01 13:25

    I believe you are trying to compare two strings representing values, the function you are looking for is:

    int atoi(const char *nptr);
    

    or

    long int strtol(const char *nptr, char **endptr, int base);
    

    these functions will allow you to convert a string to an int/long int:

    int val = strtol("555", NULL, 10);
    

    and compare it to another value.

    int main (int argc, char *argv[])
    {
        long int val = 0;
        if (argc < 2)
        {
            fprintf(stderr, "Usage: %s number\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    
        val = strtol(argv[1], NULL, 10);
        printf("%d is %s than 555\n", val, val > 555 ? "bigger" : "smaller");
    
        return 0;
    }
    

提交回复
热议问题