Detailed Explanation of C++ Integer Types

Basic Integer Types

C++ provides various integer types, arranged in increasing order of width:

Type Typical Size Value Range
char 1 byte -128 to 127 or 0 to 255
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 4 or 8 bytes Depends on the system
long long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Code Example

#include <iostream>
#include <climits>  // For getting limits of integer types

using namespace std;

int main() {
    // 1. Declaration and initialization of different integer types
    short smallNumber = 100;
    int mediumNumber = 50000;
    long largeNumber = 1000000L;
    long long veryLargeNumber = 10000000000LL;
    
    // 2. Signed and unsigned integers
    signed int negativeNumber = -50;        // Can store negative numbers
    unsigned int positiveOnly = 200;        // Can only store non-negative numbers
    
    // 3. Display sizes and ranges of various integer types
    cout << "=== Integer Type Information ===" << endl;
    cout << "short size: " << sizeof(short) << " bytes" << endl;
    cout << "short range: " << SHRT_MIN << " to " << SHRT_MAX << endl;
    
    cout << "int size: " << sizeof(int) << " bytes" << endl;
    cout << "int range: " << INT_MIN << " to " << INT_MAX << endl;
    
    cout << "long size: " << sizeof(long) << " bytes" << endl;
    cout << "long range: " << LONG_MIN << " to " << LONG_MAX << endl;
    
    cout << "long long size: " << sizeof(long long) << " bytes" << endl;
    cout << "long long range: " << LLONG_MIN << " to " << LLONG_MAX << endl;
    
    // 4. Range of unsigned types
    cout << "\n=== Unsigned Type Ranges ===" << endl;
    cout << "unsigned int range: 0 to " << UINT_MAX << endl;
    cout << "unsigned short range: 0 to " << USHRT_MAX << endl;
    
    // 5. Practical usage example
    cout << "\n=== Practical Usage Example ===" << endl;
    
    // Calculate the sum of two integers
    int a = 15, b = 27;
    int sum = a + b;
    cout << a << " + " << b << " = " << sum << endl;
    
    // Use unsigned integer for non-negative values
    unsigned int population = 1400000000;
    cout << "Approximate population of China: " << population << " people" << endl;
    
    // Use long long for large numbers
    long long distanceToMoon = 384400000LL;  // Distance to the moon (in meters)
    cout << "Distance from Earth to Moon: " << distanceToMoon << " meters" << endl;
    
    // 6. Integer overflow example
    cout << "\n=== Integer Overflow Example ===" << endl;
    short maxShort = SHRT_MAX;
    cout << "short max value: " << maxShort << endl;
    maxShort += 1;  // Overflow
    cout << "Value after overflow: " << maxShort << " (This is an incorrect result!)" << endl;
    
    // 7. Integer representation in different bases
    cout << "\n=== Different Base Representations ===" << endl;
    int decimal = 100;      // Decimal
    int octal = 0144;       // Octal (starts with 0)
    int hexadecimal = 0x64; // Hexadecimal (starts with 0x)
    int binary = 0b1100100; // Binary (C++14 support)
    
    cout << "Decimal 100: " << decimal << endl;
    cout << "Octal 0144: " << octal << endl;
    cout << "Hexadecimal 0x64: " << hexadecimal << endl;
    cout << "Binary 0b1100100: " << binary << endl;
    
    // 8. Type conversion example
    cout << "\n=== Type Conversion ===" << endl;
    int bigNumber = 100000;
    short smallContainer = bigNumber;  // Implicit conversion, may lose data
    cout << "Big number: " << bigNumber << endl;
    cout << "After converting to short: " << smallContainer << " (Data lost!)" << endl;
    
    // Explicit type conversion
    double pi = 3.14159;
    int intPi = static_cast<int>(pi);  // C++ style conversion
    cout << "Pi: " << pi << endl;
    cout << "Converted to integer: " << intPi << endl;
    
    return 0;
}

Advice on Choosing the Right Integer Type

#include <iostream>

// Example of selecting integer types based on requirements
class IntegerSelection {
public:
    // For small range values (-32768 to 32767)
    static short calculateScore(int correct, int total) {
        return static_cast<short>((correct * 100) / total);
    }
    
    // For general purposes, use int (most natural size)
    static int calculateSum(int a, int b) {
        return a + b;
    }
    
    // For large values, use long long
    static long long calculateFactorial(int n) {
        long long result = 1;
        for (int i = 1; i <= n; ++i) {
            result *= i;
        }
        return result;
    }
    
    // For non-negative values, use unsigned types
    static unsigned int countItems(unsigned int items[], int size) {
        unsigned int count = 0;
        for (int i = 0; i < size; ++i) {
            count += items[i];
        }
        return count;
    }
};

int main() {
    // Demonstrate selecting appropriate types
    short score = IntegerSelection::calculateScore(8, 10);
    cout << "Score: " << score << "%" << endl;
    
    int sum = IntegerSelection::calculateSum(100, 200);
    cout << "Sum: " << sum << endl;
    
    long long factorial = IntegerSelection::calculateFactorial(10);
    cout << "10 factorial: " << factorial << endl;
    
    unsigned int items[] = {5, 10, 3, 8};
    unsigned int total = IntegerSelection::countItems(items, 4);
    cout << "Total items: " << total << endl;
    
    return 0;
}

Output Example

=== Integer Type Information ===
short size: 2 bytes
short range: -32768 to 32767
int size: 4 bytes
int range: -2147483648 to 2147483647
long size: 8 bytes
long range: -9223372036854775808 to 9223372036854775807
long long size: 8 bytes
long long range: -9223372036854775808 to 9223372036854775807

=== Unsigned Type Ranges ===
unsigned int range: 0 to 4294967295
unsigned short range: 0 to 65535

=== Practical Usage Example ===
15 + 27 = 42
Approximate population of China: 1400000000 people
Distance from Earth to Moon: 384400000 meters

Key Points Summary

  1. Select the appropriate type: Choose the most suitable type based on the value range to save memory
  2. Be aware of overflow: Integer operations may cause overflow, requiring special attention to boundary cases
  3. Signed vs Unsigned: Choose based on whether negative numbers are needed
  4. Type conversion: Be cautious of implicit conversions that may lead to data loss
  5. Portability: The size of long type depends on the system, so be mindful when writing cross-platform code

By choosing integer types wisely, you can write efficient and reliable C++ programs.

Leave a Comment