`sizeof` — C++ Keyword

`sizeof` — C++ Keyword

The sizeof keyword in C++: yields the size in bytes of a type or expression.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

sizeof

A compile-time operator that yields the size in bytes (as std::size_t) of a type or expression. The expression operand is not evaluated.

Syntax

sizeof(type)
sizeof expression
sizeof...(pack)    // C++11 parameter pack size

Example

#include <print>
#include <cstddef>

struct Point { float x, y; };

int main() {
    std::println("{}", sizeof(int));          // typically 4
    std::println("{}", sizeof(double));       // typically 8
    std::println("{}", sizeof(Point));        // 8 (2 × float)

    int arr[10];
    std::println("{}", sizeof arr);           // 40 (10 × 4)
    std::println("{}", sizeof arr / sizeof arr[0]);  // 10 (element count)

    // Expression is unevaluated
    int x = 0;
    std::println("{}", sizeof(x++));  // 4 – x is NOT incremented
    std::println("{}", x);            // 0
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}