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

ความแตกต่างระหว่าง int และ long ใน C++

ภาพรวม

int และ long เป็น Signed Integer Types ใน C++ ความแตกต่างหลักคือ **ขนาด (Size)** และ **ช่วงค่า (Range)** ซึ่งขึ้นอยู่กับ Platform/Compiler (Implementation-Defined)

ข้อกำหนดขั้นต่ำของ C++ Standard

TypeMinimum SizeMinimum RangeTypical (64-bit Linux/macOS)Typical (Windows 64-bit)
int16 bits (2 bytes)-32,768 to 32,76732 bits (4 bytes)32 bits (4 bytes)
long32 bits (4 bytes)-2,147,483,648 to 2,147,483,64764 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

TypeSuffixตัวอย่าง
int(none)42
unsigned intU / u42U
longL / l42L
unsigned longUL / ul42UL
long longLL / ll42LL
unsigned long longULL / ull42ULL

แนวทางปฏิบัติที่ดีที่สุด (Best Practices)

  1. ใช้ int สำหรับการเขียนโปรแกรมทั่วไป (Loop Counters, Indices, Small Values)
  2. หลีกเลี่ยง long สำหรับ Portable Code เพราะขนาดต่างกันระหว่าง Windows/Linux
  3. ใช้ <cstdint> Fixed-Width Types เมื่อต้องการขนาดแน่นอน:
    • int32_t, uint32_t (แน่นอน 32-bit)
    • int64_t, uint64_t (แน่นอน 64-bit)
    • intptr_t, uintptr_t (ขนาด Pointer)
  4. ใช้ size_t สำหรับ Array Indices, Sizes (Unsigned, Match Pointer Size)
  5. ใช้ 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 แน่นอน