标签:
先上两个解释我的疑惑的链接:
http://en.cppreference.com/w/cpp/language/operator_arithmetic
开始我是看 <<Expert C programming -- Deep C Secrets>>这本书(中文译作 C专家编程), chapter 1 里面的how quite is a quite change 这一小节, 有这样一段代码:
}
然后改成这样
#include <stdio.h>
gcc 4.9.1编译运行(未加任何特殊编译选项)的结果是打印第二条. 而vs2013默认编译不通过, error:负数转变成了无符号数.
开始看 <<Expert C programming -- Deep C Secrets>>这本书这里时有点偷懒, 只记得了这两句话:
Operands with different types get converted when you do arithmetic. Everything is converted to the type of the floatest, longest operand, signed if possible without losing bits.
实际上完整的规则还是本文开头的哪两个链接靠谱.
我觉得 best practice应当是尽量少用强制转换, 谁想去记忆那些无聊的规则.
关于 C 的 arithmetic conversion (进行 算术运算 时的 强制转换规则)
标签:
原文地址:http://www.cnblogs.com/likeatree/p/4345159.html