是从转换到无符号未定义签? [英] Is conversion from unsigned to signed undefined?

查看:144
本文介绍了是从转换到无符号未定义签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 无效乐趣(){
    符号int a = -5;
    unsigned int类型B = -5;
    的printf(b的值为%U \\ N,B);
    如果(A == B)
         的printf(同一\\ n);
    其他
         的printf(差异);
}

有打印:

4294967291

在2号线符号值转换为无值。因此B为UINTMAX + 1 - 5 = 4294967291

我的问题是什么是在比较操作发生的事情。

1)是再转换为无符号,并用b相比?

2)的B(即无符号)是有史以来强制转换为符号的值,并自动比较?

3)从符号转换为签订由于未定义为INT溢出?

我已阅读的话题其他职位。我只是想澄清的问题2和3。


解决方案

  

1)是再转换为无符号比较用b?


是的。在EX pression(一== b)中,所谓的平衡的隐式类型转换发生(正式名称是通常的算术转换)。均衡规则规定如果一个签名和相同尺寸和类型的无符号的操作数进行比较,已签名的操作数转换为一无符号的


  

2)的B(即无符号)是有史以来强制转换为符号的值,并自动比较?


没有,它永远不会被转换到你的例子签署。


  

3)从符号转换为签订由于未定义为INT溢出?


这是标准所说的:(C11)


  

6.3.1.3符号和无符号整数,


  
  

1时带着整数类型的值被转换为另一种类型的整数
  比_Bool其它,如果该值可以被重新$ P $由新型psented,它
  是不变的。


  
  

2,否则,如果新类型是无符号的值是
  通过反复增加或减去一个比多个转换
  可以重新在新的类型psented直到价值$ P $最大值
  是在新的类型的范围。


  
  

3否则,新的类型是
  签名并且该值不能重新在它psented $ P $;任一结果是
  实现定义或实现定义的信号提高。


在换句话说,如果编译器可以管理做在2的转化)以上,则行为是明确的。如果不能,那么结果依赖于编译器实现。

这不是未定义的行为。

void fun(){
    signed int a=-5;
    unsigned int b=-5;
    printf("the value of b is %u\n",b);
    if(a==b)
         printf("same\n");
    else
         printf("diff");
}

It is printing :

4294967291

same

In the 2nd line signed value is converted to unsigned value. So b has the value UINTMAX + 1 - 5 = 4294967291.

My question is what is happening in the comparison operation .

1) Is a again converted to unsigned and compared with b ?

2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

3) Is conversion from unsigned to signed undefined due to int overflow ?

I have read other posts on the topic. I just want clarification on questions 2 and 3 .

解决方案

1) Is a again converted to unsigned and compared with b ?

Yes. In the expression (a==b), the implicit type conversion called "balancing" takes place (the formal name is "the usual arithmetic conversions"). Balancing rules specify that if a signed and a unsigned operand of the same size and type are compared, the signed operand is converted to a unsigned.

2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

No, it will never be converted to signed in your example.

3) Is conversion from unsigned to signed undefined due to int overflow ?

This is what the standard says: (C11)

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; either the result is implementation-defined or an implementation-defined signal is raised.

In other words, if the compiler can manage to do the conversion in 2) above, then the behavior is well-defined. If it cannot, then the result depends on the compiler implementation.

It is not undefined behavior.

这篇关于是从转换到无符号未定义签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆