Header <limits.h> (C) และ <climits> (C++) กำหนด Macro Constants สำหรับขีดจำกัดของชนิดข้อมูลพื้นฐาน (Fundamental Types) ช่วยให้เขียนโค้ดที่ Portable และไม่ขึ้นกับ Platform
Macros สำคัญใน limits.h / climits
Character Types
| Macro | คำอธิบาย | ค่าตัวอย่าง (Typical) |
CHAR_BIT | Bits per Byte | 8 |
CHAR_MIN | Min value of char | -128 (signed) / 0 (unsigned) |
CHAR_MAX | Max value of char | 127 (signed) / 255 (unsigned) |
SCHAR_MIN | Min of signed char | -128 |
SCHAR_MAX | Max of signed char | 127 |
UCHAR_MAX | Max of unsigned char | 255 |
Integer Types
| Macro | คำอธิบาย | ค่าตัวอย่าง (32-bit) |
INT_MIN | Min of int | -2,147,483,648 |
INT_MAX | Max of int | 2,147,483,647 |
UINT_MAX | Max of unsigned int | 4,294,967,295 |
SHRT_MIN | Min of short | -32,768 |
SHRT_MAX | Max of short | 32,767 |
USHRT_MAX | Max of unsigned short | 65,535 |
LONG_MIN | Min of long | -2,147,483,648 (32-bit) / -9e18 (64-bit) |
LONG_MAX | Max of long | 2,147,483,647 (32-bit) / 9e18 (64-bit) |
ULONG_MAX | Max of unsigned long | 4,294,967,295 (32-bit) / 1.8e19 (64-bit) |
LLONG_MIN | Min of long long (C99) | -9,223,372,036,854,775,808 |
LLONG_MAX | Max of long long (C99) | 9,223,372,036,854,775,807 |
ULLONG_MAX | Max of unsigned long long (C99) | 18,446,744,073,709,551,615 |
ตัวอย่างการใช้งาน
#include <iostream>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 28;
int bits = CHAR_BIT * sizeof(x); // 8 * 4 = 32 bits
cout << "The number is : " << x << endl;
cout << "Number of bits in a byte : " << CHAR_BIT << endl;
// แสดง Binary Representation
stack<bool> s;
for (int i = 0; i < bits; i++) {
s.push(x % 2);
x = x / 2;
}
for (int i = 0; i < bits; i++) {
cout << s.top();
s.pop();
if ((i + 1) % CHAR_BIT == 0) cout << " ";
}
cout << "\n\nThe minimum value of short int : " << SHRT_MIN;
return 0;
}
ผลลัพธ์
The number is : 28
Number of bits in a byte : 8
00000000 00000000 00000000 00011100
The minimum value of short int : -32768
การใช้งานเพื่อ Portability
// แทนที่จะเขียน Magic Numbers
if (value > 2147483647) { ... } // ผิด: ผูกติด 32-bit int
// ใช้ Macro
if (value > INT_MAX) { ... } // ถูกต้อง: Portable
// ตรวจสอบ Overflow ก่อนคำนวณ
if (a > 0 && b > INT_MAX - a) {
// Overflow จะเกิดขึ้น
handle_overflow();
}
int sum = a + b;
C++ Alternative: (Type-safe)
#include <iostream>
#include <limits>
int main() {
cout << "int max: " << std::numeric_limits<int>::max() << endl;
cout << "int min: " << std::numeric_limits<int>::min() << endl;
cout << "float max: " << std::numeric_limits<float>::max() << endl;
cout << "float epsilon: " << std::numeric_limits<float>::epsilon() << endl;
cout << "double digits10: " << std::numeric_limits<double>::digits10 << endl;
return 0;
}
ข้อดีของ std::numeric_limits
- Type-safe (Template)
- รองรับ User-defined Types
- มีข้อมูลเพิ่มเติม:
epsilon(), digits10, is_integer, is_signed, has_infinity, quiet_NaN()
- ทำงานกับ
constexpr
สรุป
- C: ใช้
<limits.h> กับ Macros
- C++: ใช้
<climits> (C-style) หรือ <limits> (C++ Style - แนะนำ)
- ช่วยเขียนโค้ดที่ Portable ระหว่าง Platforms (16/32/64-bit)
- หลีกเลี่ยง Magic Numbers ในโค้ด