Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C++ Variable Types and Data Storage Fundamentals

Tech May 17 2

Basic Variables

Variable Naming Conventions

C++ encourages meaningful variable names that follow specific naming conventions:

  1. Names can only contain alphabetic characters, digits, and underscores (_)
  2. The first character cannot be a digit
  3. Case sensitivity applies (uppercase and lowercase differ)
  4. C++ keywords cannot be used as identifiers
  5. Names starting with two underscores or an underscore followed by uppercase letters are reserved for implemantation use
  6. Single underscore prefixes are reserved for global identifiers
  7. C++ imposes no length restrictions, though some platforms may have limitations

The last point differs from others because using reserved names like __internal won't cause compilation errors but leads to undefined behavior. Global identifiers refer to where names are declared, which will be covered in later chapters.

Integer Types

Integers represent whole numbers without frcational parts: 2, 98, -5286, 0. Since infinite integers require finite memory representation, C++ provides multiple integer types optimized for different program requirements.

Memory allocation determines integer range: larger memory usage allows broader value ranges. Signed types handle positive and negative values, while unsigned types only store non-negative values. Width refers to memory used for storage—more memory means wider range.

Basic integer types in increasing width: char, short, int, long, and long long (C++11). Each has signed and unsigned variants, creating 10 total options. char receives special treatment as it primarily represents characters.

Integer Type Specifications

  • short: minimum 16 bits
  • int: at least as wide as short
  • long: minimum 32 bits, at least as wide as int
  • long long: minimum 64 bits, at least as wide as long

The climits header defines symbolic constants for type limits. INT_MAX represents the maximum value for int. On Windows 7 systems, this equals 2147483647. Compiler vendors provide appropriate values in their climits files.

Unsigned Variants

Each of the four integer types has an unsigned variant that cannot store negative values but offers increased maximum values. For example, if short ranges from -32768 to +32767, its unsigned version spans 0 to 65535.

#include <iostream>
#include <climits>

int main() {
    using namespace std;
    short original_value = SHRT_MAX;
    unsigned short unsigned_val = original_value;
    
    cout << "Original: " << original_value << ", Unsigned: " << unsigned_val << endl;
    cout << "Adding 1 to both..." << endl;
    
    original_value++;
    unsigned_val++;
    
    cout << "After increment - Original: " << original_value 
         << ", Unsigned: " << unsigned_val << endl;
    
    original_value = 0;
    unsigned_val = 0;
    cout << "Resetting to zero..." << endl;
    
    original_value--;
    unsigned_val--;
    
    cout << "After decrement - Original: " << original_value 
         << ", Unsigned: " << unsigned_val << endl;
    
    return 0;
}

Integer variables behave like odometers—exceeding limits wraps values to the opposite end of the range. C++ guarantees this unsigned behavior, but doesn't ensure signed integer overflow behavior.

Character Type

The char type stores characters like letters and digits. Programming languages solve character storage through numerical encoding. Therefore, char is another integer type sufficient to represent all basic symbols—letters, digits, punctuation.

Character literals use single quotes: 'A', '5', '!'. These represent numerical encodings:

  • 'A' = 65 (ASCII)
  • 'a' = 97 (ASCII)
  • '5' = 53 (ASCII)
  • '!' = 33 (ASCII)

Escape sequences handle untypable characters: \n for newline, \" for quote within strings.

Signed vs Unsigned Char

Unlike int, char is neither explicitly signed nor unsigned by default—it depends on the C++ implementation. For specific behavior, use explicit declarations:

char test_var;          // could be signed or unsigned
unsigned char pos_val;  // definitely unsigned
signed char neg_val;    // definitely signed

For numerical use, differences matter: unsigned char typically ranges 0-255, signed char ranges -128 to 127.

Boolean Type

bool variables hold true or false. C++ interprets non-zero as true and zero as false.

Floating-Point Numbers

Floating-point types represent fractional values with broader ranges than integers. C++ offers three: float, double, long double, distinguished by significant figures and exponent range.

Significant figures indicate meaningful digits. For example, 14179 feet uses 5 significant figures, while approximately 14000 feet uses 2 significant figures.

Requirements: float minimum 32 bits, double minimum 48 bits (not less than float), long double at least as many bits as double. Typically: float 32-bit, double 64-bit, long double 80-128 bit.

Scientific notation uses E-notation: 3.45E6 means 3.45 × 10⁶ = 3450000. The decimal point moves right for positive exponents, left for negative ones.

Floating-point advantages over integers: fractional values and broader ranges. Disadvantages: slower operations and reduced precision.

Type Conversion

C++'s rich type system requires automatic conversions for complex operations. When adding short values versus long values, hardware instructions differ. With 11 integer and 3 floating-point types, C++ handles many conversion scenarios:

  • Assignment between arithmetic types
  • Mixed-type expressions
  • Function parameter passing

Conversion hierarchy for expressions:

  1. If any operand is long double, convert others to long double
  2. If any operand is double, convert others to double
  3. If any operand is float, convert others to float
  4. All remaining are integer types—perform integer promotion
  5. For same-signedness operands, convert to higher-level type
  6. If mixed signedness and unsigned level higher, convert signed to unsigned
  7. If signed type covers all unsigned values, convert unsigned to signed
  8. Otherwise, convert both to unsigned version of signed type

Summary

C++ fundamental types divide into integer-stored and floating-point-stored groups. Integer types vary by storage memory and signedness. From smallest to largest: bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long (C++11). wchar_t position depends on implementation.

Characters use numerical encoding. I/O systems determine interpretation as character or number. Floating-point types handle fractional values and larger ranges than integers.

C++ provides arithmetic operators: addition, subtraction, multiplication, division, modulus. Operator precedence and associativity rules determine execution order. Type conversions occur during assignment, mixed-type operations, and casting. Most conversions remain "safe" without data loss.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.