alignof (since C11)

From cppreference.com
< c

Template:c/align/alignof/navbar

Queries alignment requirements of a type.

Contents

[edit] Syntax

alignof( type-name )

[edit] Expanded value

a value of type size_t.

[edit] Explanation

Returns the alignment, in bytes, required for any instance of the type indicated by type-name, which is either complete type or array type.

If the type is array type, alignment requirement of the element type is returned.

The types char, signed char, and unsigned char have the weakest (smallest) alignment supported by the implementation.

[edit] Keywords

_Alignof

[edit] Example

#include <stdio.h>
#include <stdalign.h>
 
struct foo
{
    int f2;
    float f1;
    char c;
};
 
int array_a[5];
double array_b[10];
 
int main(void)
{
    printf("alignment of char:         %zu\n", alignof(char));
    printf("alignment of pointer:      %zu\n", alignof(int*));
    printf("alignment of struct foo:   %zu\n", alignof(struct foo));
    printf("alignment of array_a:      %zu\n", alignof(array_a));
    printf("alignment of array_b:      %zu\n", alignof(array_b));
}

Possible output:

alignment of char:         1
alignment of pointer:      8
alignment of struct foo:   4
alignment of array_a:      4
alignment of array_b:      8

[edit] See also

alignment requirement restricts the addresses at which an object may be allocated
(C11)
set the alignment requirement of a type or an object
(function macro)
C++ documentation for alignof operator