内容简介:在csapp书中第二章有几个经常犯错的例子:
为了定义转换规则,C99允许每个整数类型具有"整数转换等级"。下面按从最高级到最低级的顺序排列。
(1)long long int、usigned long long int
(2)long int、unsigned long int
(3) int 、unsigned int
(4) short int 、unsigned short int
(5) char、signed char、unsigned char
(6)_Bool
比较转换规则如下:
如果两个操作数类型相同,过程结束,否则依次尝试下面的规则:
(1):如果两个操作数都是有符号型或者都是无符号型,将整数转换等级较低的操作数转换为等级较高的操作数类型;
(2):如果无符号操作数的等级高于或等于有符号操作数的等级,将有符号操作数转换为无符号操作数的类型。比如比较unsigned int 与int,将int提升为unsigned int.
(3):如果有符号操作数类型可以表示无符号操作数类型的所有值,将无符号操作数转换为有符号操作数的类型。比如比较int 与unsigned short int,将unsigned short int提升为int
(4):将两个操作数都转换为与有符号操作数的类型相对应的无符号类型
stackoverflow上针对这一转换规则的一个回答 stackoverflow
.
几个容易犯错的地方:#include <stdio.h> #include <stdlib.h> int main() { int a = -1; unsigned int b = 1; if(a > b) printf("a > b, a = %d, b = %u\n", a, b); else printf("a <= b, a = %d, b = %u\n", a, b); exit(0); }
按照我们上面的规则比较unsigned int 与int 时将int 提升为unsigned int ,由于-1的底层表示为全一,因此转换为unsigned int时将为UINT_MAX即4294967295.因此b>a.
编译时添加选项-Wsign-compare可以检测出该问题。[root@centos-linux-7 workspace]# gcc -Wsign-compare -g -o compare compare.c compare.c: In function ‘main’: compare.c:9:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if(a > b)
在csapp书中第二章有几个经常犯错的例子:
float sum_element(float a[],unsigned length) { int i; float result = 0; for(i=0;i<=length-1;i++) result+=a[i]; return result; }
size_t strlen(const char*s); int strlonger(char *s,char *t) { return strlen(s)-strlen(t)>0; }
都是类型惹的祸——小心unsigned
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Android 多国语言转换 Excel 和 Excel 转换为 string
- 把自然语言文本转换为向量
- go语言中int和byte转换
- C语言实现任意进制转换代码及解析
- python中数字与C语言中double结构转换
- Go语言的断言返回值和类型转换的区别
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to the Analysis of Algorithms
Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99
This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!