Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. sizeof

sizeof

The sizeof operator returns the size, in bytes, that a data type or variable occupies in memory. It lets you write portable code without hardcoding environment-specific sizes.

Syntax

// Returns the size of a type in bytes.
sizeof(type)

// Returns the size of a variable in bytes.
sizeof(variable)
sizeof variable  // Parentheses can be omitted for variables.

// Returns the total size of an array in bytes.
sizeof(array)

// Common idiom to get the number of elements in an array.
sizeof(array) / sizeof(array[0])

Common Type Sizes (64-bit environment, approximate)

TypeSizeDescription
char1 byteIn C, sizeof(char) is always defined as 1.
short2 bytes2 bytes on virtually all platforms.
int4 bytes4 bytes on most platforms, though the standard only requires it to be at least as wide as short.
long4–8 bytes4 bytes on 64-bit Windows; 8 bytes on 64-bit Linux/macOS.
long long8 bytesIntroduced in C99. Guaranteed to be at least 64 bits.
float4 bytesIEEE 754 single-precision floating-point.
double8 bytesIEEE 754 double-precision floating-point.
pointer4–8 bytes4 bytes in a 32-bit environment; 8 bytes in a 64-bit environment.

Sample Code

#include <stdio.h>

int main(void) {
    // Print the size of each type.
    printf("char:      %zu bytes\n", sizeof(char));
    printf("short:     %zu bytes\n", sizeof(short));
    printf("int:       %zu bytes\n", sizeof(int));
    printf("long:      %zu bytes\n", sizeof(long));
    printf("long long: %zu bytes\n", sizeof(long long));
    printf("float:     %zu bytes\n", sizeof(float));
    printf("double:    %zu bytes\n", sizeof(double));

    // Check the size of variables.
    int n = 42;
    double x = 3.14;
    printf("size of n: %zu bytes\n", sizeof n);
    printf("size of x: %zu bytes\n", sizeof x);

    // Get the total size and element count of an array.
    int scores[10];
    printf("array size:    %zu bytes\n", sizeof(scores)); // 40 bytes (10 × 4)
    printf("element count: %zu\n", sizeof(scores) / sizeof(scores[0])); // 10

    // Pointer size depends on the address width.
    int *p = &n;
    printf("pointer size: %zu bytes\n", sizeof(p)); // 8 on a 64-bit environment

    // Using sizeof with dynamic memory allocation (combined with malloc).
    // int *arr = malloc(sizeof(int) * 100);

    return 0;
}

Notes

sizeof is evaluated at compile time (except when used with variable-length arrays). Its return type is size_t, an unsigned integer type. Use %zu with printf to print it.

If you assign an array to a pointer or pass it to a function, sizeof returns the size of the pointer itself, not the array. You can only get the full array size when passing the array directly. This behavior is known as "array-to-pointer decay."

When using dynamic memory allocation with malloc(), always use sizeof(type) instead of hardcoding the size — this improves portability. You can also use sizeof with structs, but be aware that the compiler may insert padding bytes for alignment purposes.

If you find any errors or copyright issues, please .