ภาพรวม
int และ long เป็น Signed Integer Types ใน C++ ความแตกต่างหลักคือ **ขนาด (Size)** และ **ช่วงค่า (Range)** ซึ่งขึ้นอยู่กับ Platform/Compiler (Implementation-Defined)
ข้อกำหนดขั้นต่ำของ C++ Standard
| Type | Minimum Size | Minimum Range | Typical (64-bit Linux/macOS) | Typical (Windows 64-bit) |
|---|---|---|---|---|
int | 16 bits (2 bytes) | -32,768 to 32,767 | 32 bits (4 bytes) | 32 bits (4 bytes) |
long | 32 bits (4 bytes) | -2,147,483,648 to 2,147,483,647 | 64 bits (8 bytes) | 32 bits (4 bytes) |
long long (C++11) | 64 bits (8 bytes) | -9.22×10¹⁸ to 9.22×10¹⁸ | 64 bits (8 bytes) | 64 bits (8 bytes) |
กฎ: sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)
ตัวอย่างโค้ดตรวจสอบขนาด
#include <iostream>
#include <climits>
#include <cstdint>
using namespace std;
int main() {
cout << "=== Sizes ===\n";
cout << "sizeof(int) = " << sizeof(int) << " bytes\n";
cout << "sizeof(long) = " << sizeof(long) << " bytes\n";
cout << "sizeof(long long) = " << sizeof(long long) << " bytes\n";
cout << "sizeof(intptr_t) = " << sizeof(intptr_t) << " bytes (Pointer Size)\n";
cout << "\n=== Ranges ===\n";
cout << "INT_MIN = " << INT_MIN << ", INT_MAX = " << INT_MAX << "\n";
cout << "LONG_MIN = " << LONG_MIN << ", LONG_MAX = " << LONG_MAX << "\n";
cout << "LLONG_MIN = " << LLONG_MIN << ", LLONG_MAX = " << LLONG_MAX << "\n";
return 0;
}
ผลลัพธ์ตัวอย่าง (Linux 64-bit)
=== Sizes ===
sizeof(int) = 4 bytes
sizeof(long) = 8 bytes
sizeof(long long) = 8 bytes
sizeof(intptr_t) = 8 bytes (Pointer Size)
=== Ranges ===
INT_MIN = -2147483648, INT_MAX = 2147483647
LONG_MIN = -9223372036854775808, LONG_MAX = 9223372036854775807
LLONG_MIN = -9223372036854775808, LLONG_MAX = 9223372036854775807
ผลลัพธ์ตัวอย่าง (Windows 64-bit)
sizeof(int) = 4 bytes
sizeof(long) = 4 bytes <-- ต่างจาก Linux!
sizeof(long long) = 8 bytes
ตัวอย่างการใช้งาน
// int: ใช้ทั่วไป สำหรับตัวนับ, Index, ค่าที่ไม่ใหญ่
int count = 100;
for (int i = 0; i < count; i++) { ... }
// long: เมื่อต้องการขนาดใหญ่กว่า int (บน Linux 64-bit)
long bigNumber = 5000000000L; // 5 Billion - ต้องมี L suffix
// long long: รับประกัน 64-bit ทุก Platform (C++11)
long long veryBig = 9000000000000000000LL; // LL Suffix
// Best Practice: ใช้ Fixed-Width Types ()
int32_t exact32 = 100; // แน่นอน 32-bit
int64_t exact64 = 5000000000L; // แน่นอน 64-bit
uint32_t unsigned32 = 4000000000U;
Literals และ Suffixes
| Type | Suffix | ตัวอย่าง |
|---|---|---|
| int | (none) | 42 |
| unsigned int | U / u | 42U |
| long | L / l | 42L |
| unsigned long | UL / ul | 42UL |
| long long | LL / ll | 42LL |
| unsigned long long | ULL / ull | 42ULL |
แนวทางปฏิบัติที่ดีที่สุด (Best Practices)
- ใช้
intสำหรับการเขียนโปรแกรมทั่วไป (Loop Counters, Indices, Small Values) - หลีกเลี่ยง
longสำหรับ Portable Code เพราะขนาดต่างกันระหว่าง Windows/Linux - ใช้
<cstdint>Fixed-Width Types เมื่อต้องการขนาดแน่นอน:int32_t,uint32_t(แน่นอน 32-bit)int64_t,uint64_t(แน่นอน 64-bit)intptr_t,uintptr_t(ขนาด Pointer)
- ใช้
size_tสำหรับ Array Indices, Sizes (Unsigned, Match Pointer Size) - ใช้
ptrdiff_tสำหรับ Pointer Differences
สรุป
int: มัก 32-bit ทุก Platform (Safe Default)long: 32-bit Windows, 64-bit Linux/macOS → Not Portable!long long: 64-bit ทุก Platform (C++11)- แนะนำ: ใช้
intสำหรับทั่วไป,int64_t/uint64_tสำหรับ 64-bit แน่นอน