The sizeof operator

Previous Table of Contents "New C Standard" commentary

1118 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

1119 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

1120 The size is determined from the type of the operand.

1121 The result is an integer.

1122 If the type of the operand is a variable length array type, the operand is evaluated;

1123 otherwise, the operand is not evaluated and the result is an integer constant.

1124 When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

1125 When applied to an operand that has array type, the result is the total number of bytes in the array.85)

1126 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.

1127 The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

1128 EXAMPLE 1 A principal use of the sizeof operator is in communication with routines such as storage allocators and I/O systems. A storage-allocation function might accept a size (in bytes) of an object to allocate and return a pointer to void. For example:

        extern void *alloc(size_t);
        double *dp = alloc(sizeof *dp);

The implementation of the alloc function should ensure that its return value is aligned suitably for conversion to a pointer to double.

1129 EXAMPLE 2 Another use of the sizeof operator is to compute the number of elements in an array:

        sizeof array / sizeof array[0]

1130 EXAMPLE 3 In this example, the size of a variable length array is computed and returned from a function:

        size_t fsize3(int n)
                char b[n+3];       // variable length array
                return sizeof b;   // execution time sizeof
        int main()
                size_t size;
                size = fsize3(10); // fsize3 returns 13
                return 0;

1131 85) When applied to a parameter declared to have array or function type, the sizeof operator yields the size of the adjusted (pointer) type (see 6.9.1).

1132 Forward references: common definitions <stddef.h> (7.17), declarations (6.7), structure and union specifiers (, type names (6.7.6), array declarators (


Created at: 2008-01-30 02:39:42 The text from WG14/N1256 is copyright © ISO