Alignment requirements with alignof
suggest changeThe alignof operator yields a type's alignment requirement as a value of type size_t. C23 makes alignof a keyword. In C11 through C17, use _Alignof, or include <stdalign.h> to use its alignof macro.
An alignment requirement describes the address boundary on which objects of the type can be allocated. It is a valid alignment value supported by the implementation; do not assume a particular numeric result across platforms.
#include <stdalign.h>
#include <stddef.h>
#include <stdio.h>
struct record {
char tag;
int value;
};
int main(void)
{
printf("char: %zu\n", alignof(char));
printf("max_align_t: %zu\n", alignof(max_align_t));
printf("float[10]: %zu\n", alignof(float[10]));
printf("struct record: %zu\n", alignof(struct record));
}
The operand must name a complete object type. For an array type, the result is the alignment requirement of its element type.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents