The __alignof__ operator (IBM extension)

The __alignof__ operator is a language extension to C99 that returns the number of bytes used in the alignment of its operand. The operand can be an expression or a parenthesized type identifier. If the operand is an expression representing an lvalue, the number returned by __alignof__ represents the alignment that the lvalue is known to have. The type of the expression is determined at compile time, but the expression itself is not evaluated. If the operand is a type, the number represents the alignment usually required for the type on the target platform.

The __alignof__ operator may not be applied to the following:
  • An lvalue representing a bit field
  • A function type
  • An undefined structure or class
  • An incomplete type (such as void)
Read syntax diagramSkip visual syntax diagram
__alignof__ operator syntax

>>-__alignof__--+-unary_expression-+---------------------------><
                '-(--type-id--)----'   

If type-id is a reference or a referenced type, the result is the alignment of the referenced type. If type-id is an array, the result is the alignment of the array element type. If type-id is a fundamental type, the result is implementation-defined.

For example, on AIX®, __alignof__(wchar_t) returns 2 for a 32-bit target, and 4 for a 64-bit target.

The operand of __alignof__ can be a vector type, provided that vector support is enabled. For example,
vector unsigned int v1 = (vector unsigned int)(10);
vector unsigned int *pv1 = &v1;
__alignof__(v1); // vector type alignment: 16.
__alignof__(&v1); // address of vector alignment: 4.
__alignof__(*pv1); // dereferenced pointer to vector alignment: 16.
__alignof__(pv1); // pointer to vector alignment: 4.
__alignof__(vector signed char); // vector type alignment: 16.
When __attribute__((aligned)) is used to increase the alignment of a variable of vector type, the value returned by the __alignof__ operator is the alignment factor specified by __attribute__((aligned)).