在C / C ++中使用INT_MAX和INT_MIN
In this article, we’ll take a look at using INT_MAX and INT_MIN in C/C++.
在本文中,我们将研究在C / C ++中使用INT_MAX和INT_MIN。
These are actually useful macros which represent the maximum and minimum integer values.
这些实际上是有用的宏,它们代表最大和最小整数值。
Let’s take a look at it, using some examples.
让我们使用一些示例来看看它。
使用INT_MAX和INT_MIN (Using INT_MAX and INT_MIN)
INT_MAX is a macro which represents the maximum integer value. Similarly, INT_MIN represents the minimum integer value.
INT_MAX是代表最大整数值的宏。 同样,INT_MIN表示最小整数值。
These macros are defined in the header file <limits.h>
, so you must include it.
这些宏在头文件<limits.h>
中定义,因此您必须包括它。
#include <limits.h>INT_MAXINT_MIN
Note that any integer variable must lie between INT_MIN and INT_MAX.
请注意,任何整数变量都必须位于INT_MIN和INT_MAX之间。
Typically, integers are stored as 4 bytes (32 bits).
通常,整数存储为4个字节(32位)。
This means that in almost all machines, the maximum integer value will be 2^(31) – 1 = +2147483647.
这意味着在几乎所有计算机中,最大整数值为2 ^(31)– 1 = +2147483647。
The minimum integer value will be -(2^31) = -2147483648
最小整数值为-(2 ^ 31)= -2147483648
Let’s verify this, for our machine.
让我们为我们的机器验证一下。
#include <stdio.h>
#include <limits.h>int main() {printf("Maximum Integer Value: %d\n", INT_MAX);printf("Minimum Integer Value: %d\n", INT_MIN);return 0;
}
Output
输出量
Maximum Integer Value: 2147483647
Minimum Integer Value: -2147483648
Indeed, we get what we predict.
确实,我们得到了我们的预测。
Let’s now take another example, to correctly predict any integer overflow or underflow.
现在再举一个例子,正确预测任何整数上溢或下溢。
#include <stdio.h>
#include <limits.h>int main() {int value = 0;while (value >= 0) {// Check for overflowif (value == INT_MAX) {printf("value = %d. Possible overflow!\n", value);}value ++;}printf("Now, value = %d\n", value);value = 0;while (value <= 0) {// Check for underflowif (value == INT_MIN) {printf("value = %d. Possible underflow!\n", value);}value --;}printf("Now, value = %d\n", value);return 0;
}
Output
输出量
value = 2147483647. Possible overflow!
Now, value = -2147483648
value = -2147483648. Possible underflow!
Now, value = 2147483647
While this takes a good few seconds to run, this does indeed do what we expect.
虽然这需要花好几秒钟的时间,但确实可以达到我们的预期。
The integer will overflow to INT_MIN
, and will underflow to INT_MAX
.
整数将溢出到INT_MIN
,并将下溢到INT_MAX
。
This is useful to detect such jumps in the values.
这对于检测这些值的跳跃很有用。
为什么我们需要这些宏? (Why do we need these macros?)
Often, for certain algorithms, it is sometimes necessary to initialize a variable as the lowest/highest value.
通常,对于某些算法,有时有必要将变量初始化为最低/最高值。
The number of bits of the datatype may differ based on the machine.
数据类型的位数可能会因计算机而异。
To make the usage of the maximum/minimum values be consistent, it would be convenient if everyone could use the same macros!
为了使最大值/最小值的用法保持一致,如果每个人都可以使用相同的宏,那将很方便!
This is exactly why these kinds of macros exist –
这正是存在此类宏的原因-
- To spare you from remember the actual values为了让您免于记忆实际值
- Have consistent programming patterns across all machines在所有机器上具有一致的编程模式
- Very convenient to use使用非常方便
Hopefully, these reasons may convince you to use such kinds of macros whenever you build your own C/C++ library.
希望这些原因可以说服您在构建自己的C / C ++库时使用此类宏。
结论 (Conclusion)
In this article, we learned about using the INT_MAX and INT_MIN macros in C / C++.
在本文中,我们学习了有关在C / C ++中使用INT_MAX和INT_MIN宏的知识。
For similar content, do go through our tutorial section on C programming.
对于类似的内容,请阅读我们有关C编程的教程部分 。
参考资料 (References)
- cplusplus.com page on climitsclimits上的cplusplus.com 页面
翻译自: https://www.journaldev.com/41151/int-max-min-c-plus-plus
在C / C ++中使用INT_MAX和INT_MIN相关推荐
- C / C ++和应用程序中的INT_MAX和INT_MIN
大多数时候,在竞争性编程中,需要分配数据类型可以容纳的变量,最大值或最小值,但是记住如此大而精确的数字是一项困难的工作.因此,C ++有一些宏来表示这些数字,因此可以直接将这些宏分配给变量,而无需实际 ...
- C++ INT_MAX、INT_MIN、0x80000000以及int中负数的存储
一.INT_MAX.INT_MIN.0x80000000 32位系统中int类型占4个字节,最大值为INT_MAX(或者0x7FFFFFFF),最小值为INT_MIN(或者0x80000000). i ...
- INT_MAX和INT_MIN注意事项
[ACM]INT_MAX和INT_MIN注意事项 INT_MIN在标准头文件limits.h中定义. #define INT_MAX 2147483647 #define INT_MIN (-INT_ ...
- INT_MAX和INT_MIN的定义及使用(含溢出问题)
定义 C/C++中的 <limits.h> 头文件中定义: #define INT_MAX 2147483647 #define INT_MIN (-INT_MAX - 1) I ...
- C++的 INT_MAX 和 INT_MIN
本文目录 1. INT_MAX 和 INT_MIN 是什么 2. INT_MAX(INT_MIN)的用途 3. C语言中的 int 范围 1. INT_MAX 和 INT_MIN 是什么 INT_MA ...
- INT_MAX INT_MIN及其运算
转自:知乎阿贵 C++中常量INT_MAX和INT_MIN分别表示最大.最小整数,定义在头文件limits.h中. #define INT_MAX 2147483647 #define INT_MIN ...
- INT_MIN的用法
INT_MIN在标准头文件limits.h中定义. #include<limits.h> C/C++中常量INT_MAX和INT_MIN分别表示最大.最小整数,头文件是limits.h.I ...
- 赫夫曼树编码的算法及应用习题--数据结构
赫夫曼树编码的算法及应用习题 1.构造赫夫曼树的方法 1.根据给定的n个权值{w1,w2,---wn},构成n棵二叉树的集合F={T1,T2...,Tn},其中每棵二叉树中只有一个带权为Wi的根结点, ...
- leetcode解题记录(一)
仅为自己的学习做记录 1.leetcode1:两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入 ...
最新文章
- ffmpeg综合应用示例(一)——摄像头直播
- 【AI初识境】深度学习模型中的Normalization,你懂了多少?
- Centos Another app is currently holding the yum lock
- 核心系统100%上云!全球最大流量洪峰,阿里云扛住了
- flash builder eclipse插件安装
- python用多线程可以快几倍_用了python多进程,我跑程序花费的时间缩短了4倍
- 虚拟化与私有云的区别
- 今日恐慌与贪婪指数为54 等级由贪婪转为中立
- linux tomcat部署php项目,linux修改tomcat默认访问项目的具体步骤(必看篇)
- linux pulseaudio卸载,解决Ubuntu 9.04 下 PulseAudio声音故障
- 电影院票务管理系统数据库设计
- 2021十大付费知识平台 知识付费平台排名
- 如何使用PDF转换器将PDF转换成图片
- php 港澳台、大陆身份证正则表达式
- 基于PHP服装购物网站的设计与实现
- 关于网络性能的一些指标
- unity模型制作规范
- 双向广搜(DBFS)
- 廖雪峰老师个人网站推荐
- Kup Buty Under Armour także czynników bocznych
热门文章
- mysql分组查询n条记录
- 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释...
- wuzhicms内的全局函数--load_class()
- ubuntu查看文件大小
- html5---资料查询
- 采样干扰十大滤波算法程序大全
- [转载] python中元组(tuple)用法总结
- [转载] python中join的使用
- poi报表导出4.1.0版本工具类 导出并下载
- js中的异步[Important]