Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

(limits.h) ใน C/C++: ขีดจำกัดชนิดข้อมูล

Header <limits.h> (C) และ <climits> (C++) กำหนด Macro Constants สำหรับขีดจำกัดของชนิดข้อมูลพื้นฐาน (Fundamental Types) ช่วยให้เขียนโค้ดที่ Portable และไม่ขึ้นกับ Platform

Macros สำคัญใน limits.h / climits

Character Types

Macroคำอธิบายค่าตัวอย่าง (Typical)
CHAR_BITBits per Byte8
CHAR_MINMin value of char-128 (signed) / 0 (unsigned)
CHAR_MAXMax value of char127 (signed) / 255 (unsigned)
SCHAR_MINMin of signed char-128
SCHAR_MAXMax of signed char127
UCHAR_MAXMax of unsigned char255

Integer Types

Macroคำอธิบายค่าตัวอย่าง (32-bit)
INT_MINMin of int-2,147,483,648
INT_MAXMax of int2,147,483,647
UINT_MAXMax of unsigned int4,294,967,295
SHRT_MINMin of short-32,768
SHRT_MAXMax of short32,767
USHRT_MAXMax of unsigned short65,535
LONG_MINMin of long-2,147,483,648 (32-bit) / -9e18 (64-bit)
LONG_MAXMax of long2,147,483,647 (32-bit) / 9e18 (64-bit)
ULONG_MAXMax of unsigned long4,294,967,295 (32-bit) / 1.8e19 (64-bit)
LLONG_MINMin of long long (C99)-9,223,372,036,854,775,808
LLONG_MAXMax of long long (C99)9,223,372,036,854,775,807
ULLONG_MAXMax 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 ในโค้ด