C unsigned int + int

时间:2018-03-10 00:32:17

标签: c int unsigned

为什么此代码会打印{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.3551694, "hits": [ { "_index": "sales", "_type": "_doc", "_id": "1", "_score": 1.3551694, "_source": { "tags": [ "car", "auto" ], "comments": [ { "username": "baddriver007", "comment": "This car could have better brakes" }, { "username": "dr_who", "comment": "Where's the autopilot? Can't find it" }, { "username": "ilovemotorbikes", "comment": "This car has two extra wheels" }, { "username": "baddriver007", "comment": "This is fast car" } ] } } ] }, "aggregations": { "by_sale": { "doc_count": 4, "by_user": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 1, "buckets": [ { "key": "baddriver007", "doc_count": 2, "by_nested": { "hits": { "total": 2, "max_score": 1.3551694, "hits": [ { "_shard": "[sales][3]", "_node": "-22psoQNRLa8_Y9GeHBXaw", "_index": "sales", "_type": "_doc", "_id": "1", "_nested": { "field": "comments", "offset": 3 }, "_score": 1.3551694, "_source": { "username": "baddriver007", "comment": "This is fast car" }, "_explanation": { "value": 0, "description": "Not a match", "details": [] } }, { "_shard": "[sales][3]", "_node": "-22psoQNRLa8_Y9GeHBXaw", "_index": "sales", "_type": "_doc", "_id": "1", "_nested": { "field": "comments", "offset": 0 }, "_score": 1.3551694, "_source": { "username": "baddriver007", "comment": "This car could have better brakes" }, "_explanation": { "value": 0, "description": "Not a match", "details": [] } } ] } } }, { "key": "dr_who", "doc_count": 1, "by_nested": { "hits": { "total": 1, "max_score": 1.3551694, "hits": [ { "_shard": "[sales][3]", "_node": "-22psoQNRLa8_Y9GeHBXaw", "_index": "sales", "_type": "_doc", "_id": "1", "_nested": { "field": "comments", "offset": 1 }, "_score": 1.3551694, "_source": { "username": "dr_who", "comment": "Where's the autopilot? Can't find it" }, "_explanation": { "value": 0, "description": "Not a match", "details": [] } } ] } } } ] } } } }

"greater than 0"

如果我这样做:

int main()
{
  unsigned int a = 5;
  int b = -10;
  (a + b) > 0 ? printf("greater than 0") : printf("less than 0");
}

...打印:

printf("%d\n", a + b);

4 个答案:

答案 0 :(得分:3)

无论何时在C中执行任何操作,都会根据“常用的arithemetic转换”规则(规范的第6.3.1.8节)转换参数。它们有很多,但就本例而言,重要的是:

  

对两个操作数执行整数提升。那么   以下规则适用于升级的操作数:
  如果两个操作数具有相同的类型,则不需要进一步转换   否则,如果两个操作数都有有符号整数类型或两者都有无符号   整数类型,具有较小整数转换等级类型的操作数是   转换为具有更高等级的操作数的类型   否则,如果具有无符号整数类型的操作数的等级大于或等于   等于另一个操作数的类型的等级,然后是操作数   有符号整数类型转换为带有unsigned的操作数的类型   整数类型。

intunsigned int具有相同的版权等级,因此每当您对intunsigned int执行操作时,int都会被转换到unsigned

在您的情况下,这会导致b(-10)的值变为非常大的数字。然后你添加5,它仍然非常大(但不足以回绕到零),因此>的结果为真。

答案 1 :(得分:1)

6.3.1.1 Boolean, characters, and integers6.3.1.8 Usual arithmetic conversions(谢谢Chris)

  

如果int可以表示原始类型的所有值(限制为   通过宽度,对于位字段),该值被转换为int;   否则,它将转换为unsigned int。这些被称为   整数提升.58)所有其他类型的整数不变   促销。

  

...如果具有无符号整数类型的操作数具有更高的等级或   等于另一个操作数的类型的等级,然后是操作数   带符号整数类型转换为操作数的类型   无符号整数类型。

您的添加涉及unsignedintint无法代表unsigned的所有值,因此该值会转换为unsigned int。< / p>

答案 2 :(得分:0)

您的问题

默认情况下,根据usual arithmetic conversions,您的int会升级为unsigned int

  

[...]如果具有无符号整数类型的操作数具有等级   那么,大于或等于另一个操作数的类型的等级   带有符号整数类型的操作数转换为   具有无符号整数类型的操作数。

解决方案

您需要将a投射到int,以便让这个三元组按预期工作:

((int)a + b) > 0 ? printf("greater than 0") : printf("less than 0");

答案 3 :(得分:-1)

在print语句中,您将a和b转换为已签名的int表示形式,然后再将它们添加到一起。你没有这样做是为了你的条件。