_Alignof operator

From cppreference.com
< c‎ | language

Queries the alignment requirement of its operand type.

Used when the alignment requirement of its operand type must be known.

Contents

[edit] Syntax

_Alignof( type-name ) (since C11)

Returns a value of type (an unsigned integer type) size_t.

[edit] Explanation

Yields the alignment requirement of its operand type.

[edit] Notes

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

_Alignof cannot be used with function types or incomplete types.

When applied to an array type, the result is the alignment requirement of the element type.

When applied to an operand that has structure or union type, the result is the alignment requirement of the member type with the strictest alignment requirement.

[edit] Keywords

_Alignof

[edit] Example

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

#include <stdio.h>
 
int main(void)
{
    /* Array */
    printf("_Alignof(float[10])     = %zu\n", _Alignof(float[10]));
 
    /* Structure */
    struct ints_1 {
        unsigned short int      usi;
        unsigned int             ui;
        unsigned long int       uli;
        unsigned long long int ulli;
    };
    printf("_Alignof(struct ints_1) = %zu\n", _Alignof(struct ints_1));
 
    /* Union */
    union ints_2 {
        unsigned short int      usi;
        unsigned int             ui;
        unsigned long int       uli;
        unsigned long long int ulli;
    };
    printf("_Alignof(union ints_2)  = %zu\n", _Alignof(union ints_2));
}

Possible output:

_Alignof(float[10])     = 4
_Alignof(struct ints_1) = 8
_Alignof(union ints_2)  = 8

[edit] See also

C++ documentation for alignof