“strlen(s1) - strlen(s2)” is never less than zero

后端 未结 3 1481
礼貌的吻别
礼貌的吻别 2020-12-04 10:56

I am currently writing a C program that requires frequent comparisons of string lengths so I wrote the following helper function:

int strlonger(char *s1, cha         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 11:36

    strlen returns a size_t which is a typedef for an unsigned type.

    So,

    (unsigned) 4 - (unsigned) 7 == (unsigned) - 3
    

    All unsigned values are greater than or equal to 0. Try converting the variables returned by strlen to long int.

提交回复
热议问题