简体   繁体   English

c,unsigned char和char上的等号运算符

[英]c,equality operator on unsigned char and char

why can I get "x==y" when I compare int to unsigned int 当我比较intunsigned int时为什么会得到“ x == y”

then,why can I get "a!=b" when I compare char to unsigned char ,although they do have the same bit pattern " 0xff " 那么,为什么我比较charunsigned char时为什么会得到“ a!= b” ,尽管它们确实具有相同的位模式“ 0xff

when applying equality operator,does it take variable type into consideration? 应用相等运算符时,是否要考虑变量类型?

code: 码:

#include <stdio.h>

int main() 
{
    unsigned int x = 0xFFFFFFFF;
    int y = 0xFFFFFFFF;
    printf("unsigned int x = 0xFFFFFFFF;\n");
    printf("int y = 0xFFFFFFFF;\n");
    if (x < 0)
        printf("x < 0\n");
    else
        printf("x > 0\n");
    if (y < 0)
        printf("y < 0\n");
    else
        printf("y > 0\n");
    if(x==y)
        printf("x==y\n\n");
    ///////////-- char --////////////////////////
    unsigned char a = 0xFF;
    char b = 0xFF;    
    printf("unsigned char a = 0xFF\n");
    printf("char b = 0xFF\n");
    if (a < 0)
        printf("a < 0\n");
    else
        printf("a > 0\n");
    if (b < 0)
        printf("b < 0\n");
    else
        printf("b > 0\n");
    if(a==b)
        printf("a==b\n");   
    else
        printf("a!=b\n");

}

output: 输出:

unsigned int x = 0xFFFFFFFF;
int y = 0xFFFFFFFF;
x > 0
y < 0
x==y

unsigned char a = 0xFF
char b = 0xFF
a > 0
b < 0
a!=b

From the C11 ISO/IEC 9899:201x standard: 根据C11 ISO / IEC 9899:201x标准:

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type. 否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则将带符号整数类型的操作数转换为无符号整数类型的操作数的类型。

The promotion is applied on int y = 0xFFFFFFFF before comparing with unsigned int x = 0xFFFFFFFF . 在与unsigned int x = 0xFFFFFFFF比较之前,对int y = 0xFFFFFFFF应用提升。 Promoting int y to unsigned int will keep the value 0xFFFFFFFF, which causes x == y . int y提升为unsigned int将保持值0xFFFFFFFF,这将导致x == y

On the other hand : 另一方面 :

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int ; 如果int可以表示原始类型的所有值(对于位字段,受宽度限制),则该值将转换为int; otherwise, it is converted to an unsigned int . 否则,将其转换为unsigned int。 These are called the integer promotions . 这些称为整数促销。 All other types are unchanged by the integer promotions. 整数促销未更改所有其他类型。 The integer promotions preserve value including sign. 整数促销保留包括符号在内的价值。 As discussed earlier, whether a ''plain'' char is treated as signed is implementation-defined. 如前所述,是否将“普通”字符视为已签名是实现定义的。

Which means unsigned char a = 0xFF & char b = 0xFF are both converted to signed int before comparison. 这意味着在比较之前, unsigned char a = 0xFFchar b = 0xFF都被转换为signed int However converting b will lead to sign extension which means value of b is extended to 0xFFFFFFFF == -1 causing int a = 255 to be greater than int b = -1 . 但是,转换b将导致符号扩展,这意味着b值扩展为0xFFFFFFFF == -1导致int a = 255大于int b = -1

C generally promotes integral values to int s for operations. C通常将整数值提升为int进行运算。 For unsigned char u = 0xFFu; 对于unsigned char u = 0xFFu; and signed char s = 0xFF; signed char s = 0xFF; , in evaluating u == s s is sign-extended and u is not so it is interpreted as 0xFF == -1 . ,在评估u == s s是符号扩展的,而u不是,因此将其解释为0xFF == -1

because of promotion . 因为晋升

char or short type will be promoted to int before any comparison. 在进行任何比较之前, charshort类型将被提升为int

so 所以

unsigned char a = 0xFF will be promoted to 0x000000FF(255) unsigned char a = 0xFF将被提升为0x000000FF(255)

char b = 0xFF will be promoted to 0xFFFFFFFF(-1) char b = 0xFF将提升为0xFFFFFFFF(-1)

they are not equal. 他们不平等。

Step by step: Ignore bit patterns and focus on types and values. 循序渐进:忽略位模式,将重点放在类型和值上。

0xFF is a integer constant with the value of 255 and type of int . 0xFF是一个整数常量 ,值为255,类型为int (C11 §6.4.4.1 5) (C11§6.4.4.15)

unsigned char a = 0xFF assigned 255 to an unsigned char that can represent values [0-255] on your platform. unsigned char a = 0xFF将255分配给可以代表平台上的[0-255]值的unsigned char a gets the value of 255 and type unsigned char . a获得255的值,并键入unsigned char 6.3.1.3 1 6.3.1.3 1

char b = 0xFF; assigned 255 to an char that, on your platform can represent values [-128 - 127]. 将255分配给在您的平台上可以表示值[-128-127]的char The value is converted in an implementation defined manner. 该值以实现定义的方式转换。 In OP's case, 256 is subtracted and b gets the value of -1 and type char . 在OP的情况下,将减去256, b获得值-1并输入char 6.3.1.3 3 6.3.1.3 3

When these are compared to 0, the values do not change, yet they are promoted to int . 将它们与0进行比较时,这些值不会更改,但会提升为int §6.3.1.1 2 §6.3.1.12

Of course, -1 < 0, 255 > 0 and -1 != 255. 当然,-1 <0,255> 0和-1!= 255。

A signed integer uses the most significant bit to store a negative number so in hardware-level-logic, you have asked: 有符号整数使用最高有效位来存储负数,因此在硬件级逻辑中,您已经询问:

IF (-1 != 255){ THEN // ALWAYS RUNS } IF(-1!= 255){然后//始终运行}

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

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