C++中int8_t int16_t、int32_t、int64_t、uint8_t等学习
转自:https://blog.csdn.net/yz930618/article/details/84785970
1. int_t类型
int_t 是通过typedef定义的,t表示typedef,因为跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以最有效的维护代码。
typedef unsigned char uint8_t; typedef signed char int8_t; typedef unsigned short int uint16_t; typedef short int int16_t; typedef unsigned int uint32_t; typedef int int32_t; typedef unsigned long int uint64_t; typedef long int int64_t;
int 无论在32位还是64位机器中,都是4个字节, 且带符号。
2.size_t与ssize_t
size_t主要用于计数,如sizeof函数返回值类型即为size_t。在不同位的机器中所占的位数也不同,size_t是无符号数,ssize_t是有符号数。
- 在32位机器中定义为:typedef unsigned int size_t; (4个字节)
- 在64位机器中定义为:typedef unsigned long size_t;(8个字节)
size_t多用在for循环中,
for (size_t i = 0; i < n; i++){ }
3.最大值
https://blog.csdn.net/lanyang123456/article/details/79304271
int8 -128 ~ 127
uint8 0 ~ 255
int16 -32768 ~ 32767 (3万多)
uint16 0 ~ 65535 (6万多)
int32 -2147483648 ~ 2147483647 (21亿多)
uint32 0 ~ 4294967295 (42亿多)
int64 -9223372036854775808 ~ 9223372036854775807 (922亿亿多)
uint64 0 ~ 18446744073709551615 (1844亿亿多)
4.limits
用宏定义来表示最大最小值,有时候会记不清宏定义具体是什么。
所以可以使用limit来,很方便:
#include <limits> #include <iostream> int main() { std::cout << "int max: " << std::numeric_limits<int>::max() << std::endl; std::cout << "float max: " << std::numeric_limits<float>::max() << std::endl; std::cout << "double max: " << std::numeric_limits<double>::max() << std::endl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)