You copied the Doc URL to your clipboard.

2.2 __alignof__

The __alignof__ keyword enables you to enquire about the alignment of a type or variable.

Note

This keyword is a GNU compiler extension that the ARM® compiler supports.

Syntax

__alignof__(type)

__alignof__(expr)

Where:

type

is a type

expr

is an lvalue.

Return value

__alignof__(type) returns the alignment requirement for the type, or 1 if there is no alignment requirement.

__alignof__(expr) returns the alignment requirement for the type of the lvalue expr, or 1 if there is no alignment requirement.

Example

The following example displays the alignment requirements for a variety of data types, first directly from the data type, then from an lvalue of the corresponding data type:

#include <stdio.h>

int main(void)
{
  int       var_i;
  char      var_c;
  double    var_d;
  float     var_f;
  long      var_l;
  long long var_ll;

  printf("Alignment requirement from data type:\n");
  printf("  int       : %d\n", __alignof__(int));
  printf("  char      : %d\n", __alignof__(char));
  printf("  double    : %d\n", __alignof__(double));
  printf("  float     : %d\n", __alignof__(float));
  printf("  long      : %d\n", __alignof__(long));
  printf("  long long : %d\n", __alignof__(long long));
  printf("\n");
  printf("Alignment requirement from data type of lvalue:\n");
  printf("  int       : %d\n", __alignof__(var_i));
  printf("  char      : %d\n", __alignof__(var_c));
  printf("  double    : %d\n", __alignof__(var_d));
  printf("  float     : %d\n", __alignof__(var_f));
  printf("  long      : %d\n", __alignof__(var_l));
  printf("  long long : %d\n", __alignof__(var_ll));
}

Compiling with the following command produces the following output:

armclang --target=arm-arm-none-eabi -march=armv8-a alignof_test.c -o alignof.axf
Alignment requirement from data type:
  int       : 4
  char      : 1
  double    : 8
  float     : 4
  long      : 4
  long long : 8

Alignment requirement from data type of lvalue:
  int       : 4
  char      : 1
  double    : 8
  float     : 4
  long      : 4
  long long : 8
Was this page helpful? Yes No