加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c中的signed_extension

发布时间:2020-12-16 09:36:34 所属栏目:百科 来源:网络整理
导读:我已经尝试了以下程序并将输出设置为72.我无法理解它如何输出为72. #include stdio.hint main(){ int a=0x27ea4d72; char x=a; printf("%x",x); return 0;} 解决方法 从C标准: 6.3.1.3 Signed and unsigned integers When a value with integer type is con
我已经尝试了以下程序并将输出设置为72.我无法理解它如何输出为72.

#include <stdio.h>
int main()
{
    int a=0x27ea4d72;
    char x=a;
    printf("%x",x);
    return 0;
}

解决方法

从C标准:

6.3.1.3 Signed and unsigned integers

  1. When a value with integer type is converted to another integer
    type other than _Bool,if the value can be represented by the new
    type,it is unchanged.
  2. Otherwise,if the new type is unsigned,the value is converted by
    repeatedly adding or subtracting one more than the maximum value that
    can be represented in the new type until the
    value is in the range of the new type.
  3. Otherwise,the new type is signed and the value cannot be
    represented in it; the result is
    implementation-defined.

转换属于案例#3,因为值已签名.所以结果是实现定义的.

但是,对于代码:

#include <stdio.h>

int main()
{
    unsigned long a = 0x27ea4d72UL;
    unsigned char x = a;
    printf("%x",x);
    return 0;
}

转换属于情况#2,在这种情况下,标准基本上表示结果将是0x27ea4d72%(UCHAR_MAX 1).在UCHAR_MAX为255(最常见的情况)的情况下,您将获得0x72.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读