C++中的limits.h和climits(C++基本数据类型的最值)

2024-03-28 12:48

本文主要是介绍C++中的limits.h和climits(C++基本数据类型的最值),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

昨天有同学问到有关C++里具体数值的问题,看到还是有一点点懵的,想想网上应该能找到相关资料吧,后来发现挺少的,其实在mingw(编译文件)里可以找到limits.h这个文件,具体信息如下:

/** limits.h* This file has no copyright assigned and is placed in the Public Domain.* This file is a part of the mingw-runtime package.* No warranty is given; refer to the file DISCLAIMER within the package.** Functions for manipulating paths and directories (included from io.h)* plus functions for setting the current drive.** Defines constants for the sizes of integral types.** NOTE: GCC should supply a version of this header and it should be safe to*       use that version instead of this one (maybe safer).**/#ifndef _LIMITS_H_
#define _LIMITS_H_/* All the headers include this file. */
#include <_mingw.h>/** File system limits** TODO: NAME_MAX and OPEN_MAX are file system limits or not? Are they the*       same as FILENAME_MAX and FOPEN_MAX from stdio.h?* NOTE: PATH_MAX is the POSIX equivalent for Microsoft's MAX_PATH; the two*       are semantically identical, with a limit of 259 characters for the*       path name, plus one for a terminating NUL, for a total of 260.*/
#define PATH_MAX    260/** Characteristics of the char data type.** TODO: Is MB_LEN_MAX correct?*/
#define CHAR_BIT    8
#define MB_LEN_MAX  2#define SCHAR_MIN   (-128)
#define SCHAR_MAX   127#define UCHAR_MAX   255/* TODO: Is this safe? I think it might just be testing the preprocessor,*       not the compiler itself... */
#if ('\x80' < 0)
#define CHAR_MIN    SCHAR_MIN
#define CHAR_MAX    SCHAR_MAX
#else
#define CHAR_MIN    0
#define CHAR_MAX    UCHAR_MAX
#endif/** Maximum and minimum values for ints.*/
#define INT_MAX     2147483647
#define INT_MIN     (-INT_MAX-1)#define UINT_MAX    0xffffffff/** Maximum and minimum values for shorts.*/
#define SHRT_MAX    32767
#define SHRT_MIN    (-SHRT_MAX-1)#define USHRT_MAX   0xffff/** Maximum and minimum values for longs and unsigned longs.** TODO: This is not correct for Alphas, which have 64 bit longs.*/
#define LONG_MAX    2147483647L
#define LONG_MIN    (-LONG_MAX-1)#define ULONG_MAX   0xffffffffUL#ifndef __STRICT_ANSI__
/* POSIX wants this.  */
#define SSIZE_MAX LONG_MAX
#endif#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \|| !defined(__STRICT_ANSI__)
/* ISO C9x macro names */
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (2ULL * LLONG_MAX + 1)
#endif/** The GNU C compiler also allows 'long long int'*/
#if !defined(__STRICT_ANSI__) && defined(__GNUC__)#define LONG_LONG_MAX   9223372036854775807LL
#define LONG_LONG_MIN   (-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX  (2ULL * LONG_LONG_MAX + 1)/* MSVC compatibility */
#define _I64_MIN LONG_LONG_MIN
#define _I64_MAX LONG_LONG_MAX
#define _UI64_MAX ULONG_LONG_MAX#endif /* Not Strict ANSI and GNU C compiler */#endif /* not _LIMITS_H_ */

有了这头文件后就可以减少使用0x3fffffff这种类型了。

这篇关于C++中的limits.h和climits(C++基本数据类型的最值)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/855661

相关文章

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

06 C++Lambda表达式

lambda表达式的定义 没有显式模版形参的lambda表达式 [捕获] 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 有显式模版形参的lambda表达式 [捕获] <模版形参> 模版约束 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 含义 捕获:包含零个或者多个捕获符的逗号分隔列表 模板形参:用于泛型lambda提供个模板形参的名

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

【C++高阶】C++类型转换全攻略:深入理解并高效应用

📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C++ “ 登神长阶 ” 🤡往期回顾🤡:C++ 智能指针 🌹🌹期待您的关注 🌹🌹 ❀C++的类型转换 📒1. C语言中的类型转换📚2. C++强制类型转换⛰️static_cast🌞reinterpret_cast⭐const_cast🍁dynamic_cast 📜3. C++强制类型转换的原因📝