The __alignof__ operator (IBM extension)

The __alignof__ operator is a language extension to C99 and C++03 that returns the position to which its operand is aligned. The operand can be an expression or a parenthesized type identifier. If the operand is an expression that represents an lvalue, the number that is 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 that is usually required for the type on the target platform.

The __alignof__ operator cannot be applied to the following situations:
  • An lvalue that represents a bit field
  • A function type
  • An undefined structure or class
  • An incomplete type (such as void)

__alignof__ operator syntax

Read syntax diagramSkip visual syntax diagram__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 that is returned by the __alignof__ operator is the alignment factor that is specified by __attribute__((aligned)).