Char Comparison in C

前端 未结 3 511
野性不改
野性不改 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:20

    A char variable is actually an 8-bit integral value. It will have values from 0 to 255. These are ASCII codes. 0 stands for the C-null character, and 255 stands for an empty symbol.

    So, when you write the following assignment:

    char a = 'a'; 
    

    It is the same thing as:

    char a = 97;
    

    So, you can compare two char variables using the >, <, ==, <=, >= operators:

    char a = 'a';
    char b = 'b';
    
    if( a < b ) printf("%c is smaller than %c", a, b);
    if( a > b ) printf("%c is smaller than %c", a, b);
    if( a == b ) printf("%c is equal to %c", a, b);
    

提交回复
热议问题