关于 C 的 arithmetic conversion (进行 算术运算 时的 强制转换规则)
2015-03-17 18:34
399 查看
那本书里面都有啊啊!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2015-3-17更新~~~~~~~~~~~~~~~~~~~
先上两个解释我的疑惑的链接:
http://en.cppreference.com/w/cpp/language/operator_arithmetic https://msdn.microsoft.com/en-us/library/3t4w2bkb.aspx
开始我是看 <<Expert C programming -- Deep C Secrets>>这本书(中文译作 C专家编程), chapter 1 里面的how quite is a quite change 这一小节, 有这样一段代码:
#include <stdio.h>
int main()
{
if(-1 < (unsigned char)1)
printf("-1 is less than (unsigned char)1: ANSI semantics.\n");
else
printf("-1 is NOT less than (unsigned char)1: K&R semantics.\n");
return 0;
}
我用了vs2013和gcc 4.9.1分别去编译运行, 都是ANSI的语义.打印第一条语句.
然后改成这样
#include <stdio.h>
int main()
{
if(-1 < (unsigned int)1)//或者是unsigned
printf("1111111111.\n");
else
printf("222222222222222.\n");
return 0;
}
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.
gcc警告强度开大一点就好了
sh-4.3# gcc -o main *.c -Wall
sh-4.3# gcc -o main *.c -Wall -Wextra
main.c: In function 'main':
main.c:5:11: warning: comparison between signed and unsigned integer expressions [-Wsign-com
pare]
if(-1 < (unsigned ) 1) ^
实际上完整的规则还是本文开头的哪两个链接靠谱.
我觉得 best practice应当是尽量少用强制转换, 谁想去记忆那些无聊的规则.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2015-3-17更新~~~~~~~~~~~~~~~~~~~
先上两个解释我的疑惑的链接:
http://en.cppreference.com/w/cpp/language/operator_arithmetic https://msdn.microsoft.com/en-us/library/3t4w2bkb.aspx
开始我是看 <<Expert C programming -- Deep C Secrets>>这本书(中文译作 C专家编程), chapter 1 里面的how quite is a quite change 这一小节, 有这样一段代码:
#include <stdio.h>
int main()
{
if(-1 < (unsigned char)1)
printf("-1 is less than (unsigned char)1: ANSI semantics.\n");
else
printf("-1 is NOT less than (unsigned char)1: K&R semantics.\n");
return 0;
}
我用了vs2013和gcc 4.9.1分别去编译运行, 都是ANSI的语义.打印第一条语句.
然后改成这样
#include <stdio.h>
int main()
{
if(-1 < (unsigned int)1)//或者是unsigned
printf("1111111111.\n");
else
printf("222222222222222.\n");
return 0;
}
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.
gcc警告强度开大一点就好了
sh-4.3# gcc -o main *.c -Wall
sh-4.3# gcc -o main *.c -Wall -Wextra
main.c: In function 'main':
main.c:5:11: warning: comparison between signed and unsigned integer expressions [-Wsign-com
pare]
if(-1 < (unsigned ) 1) ^
实际上完整的规则还是本文开头的哪两个链接靠谱.
我觉得 best practice应当是尽量少用强制转换, 谁想去记忆那些无聊的规则.
相关文章推荐
- 例子:3、2不同种类的整型数据可以进行算术运算
- c++算术运算和位运算中类型转换和类型提升规则和方法
- C++中算术运算的隐式转换规则
- linux命令:浅谈shell中如何进行算术运算
- c语言算术运算表达式与赋值运算表达式的类型转换规则
- 精通cobol--9.12.2 使用SET语句对索引进行算术运算
- linux shell 使用双小括号进行算术运算
- c++算术运算和位运算中类型转换和类型提升规则和方法
- 17_Shell语言―――如何在shell 脚本中进行算术运算
- java中应用Stack进行算术运算
- 关于&&和||的运算规则
- 在位集上进行算术运算
- C语言中算术运算的隐式类型转换规则
- 关于浮点数不能进行位运算的问题
- 一个关于银行卡号规则的问题,根据规则进行银行卡的验证
- 关于Java中NaN,Infinity,-Infinity参与算术运算的各种情况
- 关于不能够精确的对浮点数进行运算的问题
- 关于不能够精确的对浮点数进行运算的问题
- 关于“双或”和”单或“逻辑符的运算规则
- 关于指针的算术运算