#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
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. 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. 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.
总结以上是内存溢出为你收集整理的c中的signed_extension全部内容,希望文章能够帮你解决c中的signed_extension所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)