📅  最后修改于: 2023-12-03 14:39:58.495000             🧑  作者: Mango
在C++中,每种整数类型都有其自己的范围。在处理大型数据、计算机科学以及任何需要整数运算的情况下,了解这些范围是非常重要的。
C++中有四种基本整数类型:
此外,还有两个可拓展的整数类型:
以下是C++中整数类型的精确范围(使用<limits>
头文件):
#include <limits>
#include <iostream>
int main() {
std::cout << "char size: " << sizeof(char) << '\n';
std::cout << "char min: " << std::numeric_limits<char>::min() << '\n';
std::cout << "char max: " << std::numeric_limits<char>::max() << '\n';
}
输出:
char size: 1
char min: -128
char max: 127
#include <limits>
#include <iostream>
int main() {
std::cout << "int size: " << sizeof(int) << '\n';
std::cout << "int min: " << std::numeric_limits<int>::min() << '\n';
std::cout << "int max: " << std::numeric_limits<int>::max() << '\n';
}
输出:
int size: 4
int min: -2147483648
int max: 2147483647
#include <limits>
#include <iostream>
int main() {
std::cout << "short size: " << sizeof(short) << '\n';
std::cout << "short min: " << std::numeric_limits<short>::min() << '\n';
std::cout << "short max: " << std::numeric_limits<short>::max() << '\n';
}
输出:
short size: 2
short min: -32768
short max: 32767
#include <limits>
#include <iostream>
int main() {
std::cout << "long size: " << sizeof(long) << '\n';
std::cout << "long min: " << std::numeric_limits<long>::min() << '\n';
std::cout << "long max: " << std::numeric_limits<long>::max() << '\n';
}
输出:
long size: 8
long min: -9223372036854775808
long max: 9223372036854775807
#include <limits>
#include <iostream>
int main() {
std::cout << "long long size: " << sizeof(long long) << '\n';
std::cout << "long long min: " << std::numeric_limits<long long>::min() << '\n';
std::cout << "long long max: " << std::numeric_limits<long long>::max() << '\n';
}
输出:
long long size: 8
long long min: -9223372036854775808
long long max: 9223372036854775807
#include <limits>
#include <iostream>
int main() {
std::cout << "unsigned long long size: " << sizeof(unsigned long long) << '\n';
std::cout << "unsigned long long min: " << std::numeric_limits<unsigned long long>::min() << '\n';
std::cout << "unsigned long long max: " << std::numeric_limits<unsigned long long>::max() << '\n';
}
输出:
unsigned long long size: 8
unsigned long long min: 0
unsigned long long max: 18446744073709551615
在C++中,整数类型有各自的范围,涉及到了很多处理大型数据和计算机科学的情况。通过使用<limits>
头文件,我们可以很容易地查看这些类型的范围,确保我们的程序能够正常处理数据。