Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

alignas — alignment specifier

alignas specifies object or member alignment. In C23 it is a keyword; _Alignas remains an obsolescent spelling.

Version: C23; repository compiler flag, production runtime not attested

Syntax

alignas(type-name)
alignas(constant-expression)
_Alignas(type-name)
_Alignas(constant-expression)

alignas(T) means alignas(alignof(T)). It specifies object or member alignment without changing its type. In C23 no header is needed; _Alignas has the same meaning but is obsolescent.

A numeric argument must be an integer constant expression: a supported alignment or zero. Zero has no effect; multiple specifiers select the strictest alignment. Their combined requirement cannot weaken natural alignment. Extended alignments depend on implementation and storage duration.

Do not use it in declarations with typedef or register, or in declarations of functions, function parameters, or bit-fields. Declarations of the same object must follow alignment-consistency rules.

Examples

#include <stdio.h>

int main(void)
{
    alignas(int) unsigned char buffer[sizeof(int)] = {0};
    printf("%u\n", (unsigned)buffer[0]);
    return 0;
}

Result

0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources