【climit】INT_MIN & INT_MAX
2015-03-10 23:46
776 查看
众所周知,我们常常需要知道所谓的INF值,
那个2开头的一串数字代表int型的最大值,
虽然很多人都记住了,也有很多人用3f3f3f3f这样的方法来表示,
不过自然这种选择:limit.h里的INT_MIN & INT_MAX也常常被大家使用。
有时候会看到有人直接
这就需要吐槽了,为什么呢,在climit的定义里,它们是这样的
网上查来的资料是这么说——
这里没有简单地将INT_MIN赋值成-2147483647,是因为-2147483648对于编译器而言是个表达式,而2147483648对于32-bit整数是无法表示的,所以经过这个表达式的结果是未定义的。在GCC上直接写-2147483648后,编译器给出了警告,说结果是unsigned。
“An integer constant begins with a digit, but has no period or exponent part. It may have
a prefix that specifies its base and a suffix that specifies its type.”
The problem is that 2147483648 cannot be represented in a signed 32-bit integer, so it becomes either an unsigned long int or a long long int.
So we have to resort to a little trickery, and compute -2147483648 as (-2147483647 – 1), which all fit nicely into 32-bit signed integers, and INT_MIN gets
the right type and value.
这里我解释一下:
这样一个程序可能可以帮上忙
所以,使用的话就加上头文件climit,
然后轻松愉快地用INT_MAX和INT_MIN吧
那个2开头的一串数字代表int型的最大值,
虽然很多人都记住了,也有很多人用3f3f3f3f这样的方法来表示,
不过自然这种选择:limit.h里的INT_MIN & INT_MAX也常常被大家使用。
有时候会看到有人直接
int minValue=-INT_MAX
这就需要吐槽了,为什么呢,在climit的定义里,它们是这样的
#define INT_MAX 2147483647 #define INT_MIN (- INT_MAX - 1)
网上查来的资料是这么说——
这里没有简单地将INT_MIN赋值成-2147483647,是因为-2147483648对于编译器而言是个表达式,而2147483648对于32-bit整数是无法表示的,所以经过这个表达式的结果是未定义的。在GCC上直接写-2147483648后,编译器给出了警告,说结果是unsigned。
“An integer constant begins with a digit, but has no period or exponent part. It may have
a prefix that specifies its base and a suffix that specifies its type.”
The problem is that 2147483648 cannot be represented in a signed 32-bit integer, so it becomes either an unsigned long int or a long long int.
So we have to resort to a little trickery, and compute -2147483648 as (-2147483647 – 1), which all fit nicely into 32-bit signed integers, and INT_MIN gets
the right type and value.
这里我解释一下:
这样一个程序可能可以帮上忙
#include <stdio.h> #include <limits.h> #include <float.h> int main(void) { if (-2147483648 > 0) printf("positive\n"); if (-2147483647 - 1 < 0) printf("negative\n"); if (INT_MIN == -INT_MIN) printf("equal\n"); if (FLT_MIN > 0) printf("floating\n"); return 0; }
所以,使用的话就加上头文件climit,
然后轻松愉快地用INT_MAX和INT_MIN吧
相关文章推荐
- 一天一个C Run-Time Library 函数(2) __max & __min
- Generic<Programming>: 再谈Min和Max
- 【SPL标准库专题(7)】 Datastructures:SplHeap & SplMaxHeap & SplMinHeap
- /usr/include/linux/netfilter.h: 错误:‘in’的类型不完全,netfilter_ipv4.h:53: ‘INT_MIN’‘INT_MAX’未声明解决方法
- [LeetCode]--295. Find Median from Data Stream(Max-Heap & Min-Heap)
- INT_MAX和INT_MIN注意事项
- INT_MAX和INT_MIN注意事项
- 泛型算法系列16:max()&&min()&&min_element()&&max_element()
- std::min&std::max与mfc冲突问题
- String split(“reg”)与split("reg",int limit)
- MFC Windows 程序设计->控制窗口大小 WM_GETMINMAXINFO
- std::min&std::max与mfc冲突问题
- 程序猿之---C语言细节9(宏定义、max(a,b)宏定义细节、大小端判断、(int&)a什么意思)
- MySQL入门(3)- 分组函数(count、sum、avg、max、min)&分组查询(groupby、orderby)
- Why define INT_MIN (-INT_MAX - 1)
- compute the MAX and MIN of int
- INT_MIN和INT_MAX
- 关于Html5中<input type="number" name="points" min="1" max="10" />只能输入整数的问题
- INT_MAX,INT_MIN,isdigit函数
- INT_MIN和INT_MAX溢出问题