Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 10

Primitive Data Types - Sizes, Ranges & stdint.h

C's primitive types have implementation-defined sizes - `int` is 16 bits on some platforms and 32 bits on others. The C standard only guarantees minimum sizes and ordering relationships. For portable code, `<stdint.h>` (C99) provides exact-width types like `int32_t` and `uint64_t` - the types used in network protocols, file formats, and embedded firmware.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

All Primitive Types With sizeof

All Primitive Types With sizeof
#include <stdio.h>
#include <stdint.h>   /* C99: int8_t, uint32_t, int64_t, etc. */
#include <limits.h>   /* INT_MAX, CHAR_MIN, etc. */
#include <float.h>    /* FLT_MAX, DBL_EPSILON, etc. */

int main(void) {
    /* ── Integer family ─────────────────────────────── */
    printf("char        : %zu bytes, min=%d, max=%d
",
           sizeof(char), CHAR_MIN, CHAR_MAX);
    printf("short       : %zu bytes
", sizeof(short));
    printf("int         : %zu bytes, max=%d
", sizeof(int), INT_MAX);
    printf("long        : %zu bytes
", sizeof(long));
    printf("long long   : %zu bytes
", sizeof(long long));

    /* ── Unsigned variants (no sign bit → larger max) ── */
    unsigned int ui = 4294967295U;   /* U suffix = unsigned literal */
    printf("uint max    : %u
", ui);

    /* ── Floating-point ─────────────────────────────── */
    printf("float       : %zu bytes, ~7 decimal digits
", sizeof(float));
    printf("double      : %zu bytes, ~15 decimal digits
", sizeof(double));
    printf("long double : %zu bytes
", sizeof(long double));

    /* ── C99 exact-width types (ALWAYS use for protocols/formats) */
    int8_t   a = -128;
    uint8_t  b = 255;
    int32_t  c = 2147483647;
    uint64_t d = 18446744073709551615ULL;

    printf("int8_t  = %d  (%zu bytes)
", a, sizeof(a));
    printf("uint8_t = %u  (%zu bytes)
", b, sizeof(b));
    printf("int32_t = %d  (%zu bytes)
", c, sizeof(c));
    /* Use PRIu64 macro for portable uint64_t printing: */
    /* printf("uint64_t = %" PRIu64 "
", d); */
    (void)d; /* suppress unused warning */

    return 0;
}

Memory Layout of Variables

Memory Layout of Variables
#include <stdio.h>

int global_var = 100;     /* data segment - initialized globals */
int uninit_global;        /* BSS segment  - zero-initialized    */

int main(void) {
    int local_var = 42;   /* stack - automatic lifetime          */

    /* Print addresses to see where each lives */
    printf("global_var  @ %p (data segment)
", (void *)&global_var);
    printf("uninit_glob @ %p (BSS segment)
",  (void *)&uninit_global);
    printf("local_var   @ %p (stack)
",         (void *)&local_var);

    /* Stack addresses are typically HIGH (grow downward)
       Global/BSS are in the middle range
       Heap (malloc) is typically LOW-to-MIDDLE           */

    /* Size is determined at COMPILE TIME - not at runtime */
    printf("sizeof(int)    = %zu
", sizeof(int));
    printf("sizeof(double) = %zu
", sizeof(double));

    return 0;
}

Common Mistakes

  • Assuming `int` is always 32 bits - on 16-bit embedded systems, `int` may be 16 bits. For portable code that manipulates bit patterns, file formats, or network packets, always use `int32_t`, `uint16_t`, etc. from `<stdint.h>`.
  • Mixing signed and unsigned in comparisons - `signed int x = -1; unsigned int y = 1; if (x < y)` - this is false in C because `-1` promotes to a huge unsigned number (4294967295). Enable `-Wsign-compare` to catch this.
  • Using `sizeof` result as `int` - `sizeof` returns `size_t`, which is unsigned. Store it in `size_t`, not `int`. Comparing `int` with `size_t` results can produce warnings or silent bugs.

Tip

Tip

Practice Primitive Data Types Sizes Ranges stdinth in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Fixed-width integer types from stdint.h ensure cross-platform precision in C

Practice Task

Note

Practice Task - (1) Write a working example of Primitive Data Types Sizes Ranges stdinth from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Primitive Data Types Sizes Ranges stdinth is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • C's primitive types have implementation-defined sizes - `int` is 16 bits on some platforms and 32 bits on others.
  • Assuming `int` is always 32 bits - on 16-bit embedded systems, `int` may be 16 bits. For portable code that manipulates bit patterns, file formats, or network packets, always use `int32_t`, `uint16_t`, etc. from `<stdint.h>`.
  • Mixing signed and unsigned in comparisons - `signed int x = -1; unsigned int y = 1; if (x < y)` - this is false in C because `-1` promotes to a huge unsigned number (4294967295). Enable `-Wsign-compare` to catch this.
  • Using `sizeof` result as `int` - `sizeof` returns `size_t`, which is unsigned. Store it in `size_t`, not `int`. Comparing `int` with `size_t` results can produce warnings or silent bugs.

Topics in This Module

0/10 done