Namespaces
Variants
Views
Actions

sizeof operator

From cppreference.com
< c‎ | language

Queries size of the object or type

Used when actual size of the object must be known

Contents

[edit] Syntax

sizeof( type )
sizeof expression

Both versions return a value of type size_t.

[edit] Explanation

1) returns size in bytes of the object representation of type.

2) returns size in bytes of the object representation of the type of expression

[edit] Notes

Depending on the computer architecture, a byte may consist of 8 or more bits, the exact number being recorded in CHAR_BIT.

sizeof(char), sizeof(signed char), and sizeof(unsigned char) always return 1.

sizeof cannot be used with function types, incomplete types, or bit-field lvalues.

When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time. Otherwise, expression is not evaluated and the result is a compile-time constant. (since C99)

[edit] Keywords

sizeof

[edit] Example

The example output corresponds to a system with 64-bit pointers and 32-bit int.

#include <stdio.h>
 
int main(void)
{
    /* Size (in bytes) of the object representation of the type. */
    printf("sizeof(float)         = %2zu\n", sizeof(float));
    printf("sizeof(double)        = %2zu\n", sizeof(double));
    printf("sizeof(long double)   = %2zu\n", sizeof(long double));
 
    /* Size (in bytes) of the evaluated expression. */
    printf("sizeof 1              = %2zu\n", sizeof 1);        /* without parentheses */
    printf("sizeof(1.0F)          = %2zu\n", sizeof(1.0F));    /* with parentheses    */
    printf("sizeof(1.0F+1.0)      = %2zu\n", sizeof(1.0F+1.0));
    printf("sizeof(1.0F+1.0L)     = %2zu\n", sizeof(1.0F+1.0L));
 
    printf("sizeof(char *)        = %2zu\n", sizeof(char *));
    printf("sizeof(long double *) = %2zu\n", sizeof(long double *));
 
    /* Array */
    printf("sizeof(float[10])     = %zu\n", sizeof(float[10]));
 
    /* Structure */
    struct ints_1 {
        unsigned short int      usi;
        unsigned int             ui;
        unsigned long int       uli;
        unsigned long long int ulli;
    };
    printf("sizeof(struct ints_1) = %2zu\n", sizeof(struct ints_1));
 
    /* Union */
    union ints_2 {
        unsigned short int      usi;
        unsigned int             ui;
        unsigned long int       uli;
        unsigned long long int ulli;
    };
    printf("sizeof(union ints_2)  = %2zu\n", sizeof(union ints_2));
}

Possible output:

sizeof(float)         =  4
sizeof(double)        =  8
sizeof(long double)   = 16
sizeof 1              =  4
sizeof(1.0F)          =  4
sizeof(1.0F+1.0)      =  8
sizeof(1.0F+1.0L)     = 16
sizeof(char *)        =  8
sizeof(long double *) =  8
sizeof(float[10])     = 40
sizeof(struct ints_1) = 24
sizeof(union ints_2)  =  8