C Data Types and Variables: Ranges, Precision, and the Memory Model

    Understand C’s fundamental data types, signed and unsigned integer ranges, floating-point precision, and Boolean values. This guide also explains how variable declarations, initialization, scope, and storage duration relate to memory, with practical checks for writing portable, defensive C code.

    1. Start with the Right Mental Model: What Is a Data Type?

    Beginners often think of a data type as a limit on how large a number can be. That is only part of the picture. A more useful model is:

    Type = value range + storage size + supported operations

    Together, these properties define how the compiler interprets and operates on data.

    • Value range determines which values a type can represent. Operations or conversions outside that range may produce undefined, implementation-defined, or otherwise type-specific behavior.
    • Storage size determines how many bytes an object occupies, affecting memory layout and structure alignment.
    • Supported operations determine which operators apply, including +, -, ++, and bitwise operators, and how the types of their results are determined.

    With this model, range tables become something you can reason about rather than simply memorize.

    2. Signed and Unsigned Integers: The Sign-Bit Trade-Off

    C’s integer types have signed and unsigned forms.

    • Signed integers represent both positive and negative values. In the usual two’s-complement representation, the highest-order bit carries the sign, leaving a smaller positive range.
    • Unsigned integers represent only nonnegative values. All value bits contribute to the magnitude, giving a larger positive range at the same width.
    • For types such as int, the signed keyword is optional: int and signed int are equivalent. An unsigned declaration uses unsigned explicitly.

    Plain char is an important exception: whether it behaves as signed or unsigned is implementation-defined.

    Modern mainstream platforms use two’s complement for signed integers. This representation has one more negative value than positive values. An 8-bit signed integer therefore ranges from −128 to 127, not −127 to 127.

    Use the standard limits rather than assuming a particular range:

    1. #include <stdio.h>
    2. #include <limits.h>
    3. int main(void) {
    4. printf("int : %d ~ %d\n", INT_MIN, INT_MAX);
    5. printf("uint : 0 ~ %u\n", UINT_MAX);
    6. return 0;
    7. }

    Unsigned types can be appropriate for quantities, lengths, indexes, and counters. Signed types are appropriate for values that may be negative, such as differences, temperatures, or balance changes. Nonnegativity alone, however, does not eliminate the need to consider subtraction and mixed-type expressions.

    Avoid casually mixing signed and unsigned operands. Depending on their types and ranks, the usual arithmetic conversions can convert a signed operand to unsigned. For example, when a is an int containing -1 and b is an unsigned int containing 1U, a > b evaluates to true: a is converted to a large unsigned value before the comparison.

    3. Integer Types and Value Ranges

    For an integer type with n value-representation bits and no padding bits, the familiar ranges are:

    • Signed, using two’s complement: -2^(n-1) through 2^(n-1) - 1
    • Unsigned: 0 through 2^n - 1

    The following table shows typical ranges for a 32-bit implementation with 8-bit bytes, 16-bit short, 32-bit int and long, and 64-bit long long.

    Type Typical width Value range
    signed char 8 bits −128 to 127
    unsigned char 8 bits 0 to 255
    short int 16 bits −32,768 to 32,767
    unsigned short int 16 bits 0 to 65,535
    int 32 bits −2,147,483,648 to 2,147,483,647
    unsigned int 32 bits 0 to 4,294,967,295
    long int 32 bits −2,147,483,648 to 2,147,483,647
    unsigned long int 32 bits 0 to 4,294,967,295
    long long int 64 bits −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    unsigned long long int 64 bits 0 to 18,446,744,073,709,551,615

    C specifies minimum representable ranges and relative storage-size requirements, not one universal width for each type.

    Although int and long commonly have the same width on 32-bit systems, long is often 64 bits on 64-bit systems while int remains 32 bits. Other 64-bit implementations retain a 32-bit long. Do not assume that long must occupy more storage than int.

    For portable code:

    1. Use exact-width types from <stdint.h>, such as int32_t and uint64_t, when an exact width is required and those types are available.
    2. Obtain integer boundaries from <limits.h> rather than hard-coding them.
    3. Use sizeof to inspect storage size. Its result has the unsigned integer type size_t.

    Remember that sizeof measures bytes, not bits. A C byte is not universally required to contain eight bits.

    4. Floating-Point Types: Significant Digits Are Not Decimal Places

    Floating-point values have two separate constraints: precision and range.

    On common implementations, float provides roughly 6–7 significant decimal digits, while double provides roughly 15–16.

    • 123.4567 has seven significant digits.
    • 0.0001234567 also has seven significant digits, despite having ten digits after the decimal point.

    Significant digits are counted from the first nonzero digit, not from the decimal point.

    For common floating-point formats, positive normalized values range approximately as follows:

    • float: 1.175e-38 to 3.403e+38
    • double: 2.225e-308 to 1.798e+308

    These are not complete intervals of representable values: zero, negative values, and smaller subnormal values also exist in common implementations.

    Floating-Point Comparison

    Decimal 0.1 cannot be represented exactly in the usual binary floating-point formats. This does not mean every equality comparison involving it fails.

    1. #include <math.h>
    2. float f = 0.1f;
    3. double d = 0.1;
    4. /* This comparison is true: both sides use the same float value. */
    5. if (f == 0.1f) { /* Not an example of a rounding mismatch. */ }
    6. /* For computed results, compare the difference with a suitable tolerance. */
    7. if (fabsl(d - 0.1) < 1e-9) { /* Choose a tolerance appropriate to the calculation. */ }

    The important distinction is between comparing identical stored representations and comparing results of calculations that may accumulate rounding differences. The second condition illustrates the tolerance pattern; because d was initialized from the same literal, it does not itself demonstrate accumulated rounding error.

    Practical guidelines:

    • Prefer double by default.
    • Consider float for large arrays when memory use or bandwidth matters.
    • For money, billing, or other exact decimal requirements, use fixed-point integers—for example, storing amounts in cents—or an appropriate decimal type.
    • Pay attention to literal types: 1.0 is a floating-point constant, while 1 is an integer constant.
    • Choose comparison tolerances for the calculation rather than treating one threshold as universally reliable.

    5. Boolean Values: From Convention to Standard Support

    Early C standards had no dedicated Boolean type. Programs conventionally used 0 for false and any nonzero value for true, often storing logical values in an int.

    From C99 through C17, including <stdbool.h> provides the macros bool, true, and false, with bool referring to the standard _Bool type. In C23, these names are language keywords.

    For C99–C17 code, <stdbool.h> makes Boolean intent clearer than an integer accompanied by a comment.

    Although char is an integer type commonly used to store character codes, it is not inherently a Boolean type, nor is it universally eight bits wide. Use Boolean notation for logical state rather than repurposing char simply because it is small.

    6. Variables: Declarations, Initialization, and Lifetime

    A useful introductory memory model is a row of numbered storage cells, each representing one byte.

    Defining a variable conceptually reserves a sequence of cells and gives the object a name. Its type determines the storage size, and initialization determines its initial value.

    1. int count = 0; /* Typically occupies 4 bytes; initialized to 0. */
    2. char letter = 'A'; /* Occupies 1 C byte; initialized with a character code. */

    This is a conceptual model, not a guarantee that every variable occupies a distinct physical memory location: a compiler may optimize storage.

    Four distinctions are especially useful:

    • Initialization: An uninitialized automatic local variable has an indeterminate value. Reading it can cause undefined behavior and is a common defect. Objects with static storage duration, including global and static variables, are zero-initialized when no explicit initializer is supplied.
    • Scope: Scope determines where a variable’s name is visible. Leaving a scope removes access through that name; it does not, by itself, describe when the underlying storage ceases to exist.
    • Lifetime and storage duration: Ordinary automatic local objects generally remain alive until execution leaves their block. Objects with static storage duration exist throughout program execution.
    • Storage size: sizeof reports an object’s or type’s size in bytes, not the value stored in it or its maximum representable value.

    Keeping scope and lifetime separate prevents confusion: the visibility of a name and the existence of an object are related, but different, concepts.

    7. A Defensive Coding Checklist

    1. Use narrower types only when the required range is known. Saving storage is not worth introducing overflow.
    2. Distinguish signed overflow from unsigned wraparound. Unsigned arithmetic wraps using defined modular arithmetic; signed arithmetic overflow is undefined behavior.
    3. Use standard limits and appropriate fixed-width types. Avoid magic numbers for type boundaries.
    4. Review conversions before mixing types. Establish the intended operand types and arithmetic width. Explicit casts can document a conversion, but do not automatically make an out-of-range conversion or calculation safe.
    5. Use suitable tolerances for floating-point results. Use fixed-point or decimal representations when exact decimal arithmetic is required.
    6. Express Boolean intent directly. In C99–C17, use <stdbool.h> rather than relying on int plus explanatory comments.

    The central idea is that a type is a contract. Once that contract is clear, integer ranges, precision limits, and variable storage behavior become connected concepts rather than isolated facts. This foundation also prepares you to understand pointers, structures, and memory alignment.