Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      _Alignof(since C11)(deprecated in C23), alignof(since C23) operator

      From cppreference.com
      <c‎ |language
       
       
       
       

      Queries the alignment requirement of its operand type.

      Contents

      [edit]Syntax

      _Alignof(type-name)(since C11)(deprecated in C23)
      alignof(type-name)(since C23)

      This operator is typically used through the convenience macroalignof, which is provided in the header<stdalign.h>

      (until C23)

      [edit]Explanation

      Returns thealignment requirement of the typenamed bytype-name. Iftype-name is an array type, the result is the alignment requirement of the array element type. Thetype-name cannot be function type or an incomplete type.

      The result is an integer constant of typesize_t.

      The operand is not evaluated (so external identifiers used in the operand do not have to be defined).

      Iftype-name is aVLA type, its size expression is not evaluated.

      [edit]Notes

      The use of_Alignof(until C23)alignof(since C23) with expressions is allowed by some C compilers as a non-standard extension.

      [edit]Keywords

      alignof,_Alignof

      [edit]Example

      Run this code
      #include <stdalign.h>#include <stddef.h>#include <stdio.h> int main(void){printf("Alignment of char = %zu\n", alignof(char));printf("Alignment of max_align_t = %zu\n", alignof(max_align_t));printf("alignof(float[10]) = %zu\n", alignof(float[10]));printf("alignof(struct{char c; int n;}) = %zu\n",            alignof(struct{char c;int n;}));}

      Possible output:

      Alignment of char = 1Alignment of max_align_t = 16alignof(float[10]) = 4alignof(struct{char c; int n;}) = 4

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C standards.

      DRApplied toBehavior as publishedCorrect behavior
      DR 494C11whether the size expression in a VLA is evaluated in_Alignof was unspecifiedit is unevaluated

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.5.3.4 The sizeof and alignof operators (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.3.4 The sizeof and _Alignof operators (p: 64-65)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.3.4 The sizeof and _Alignof operators (p: 90-91)

      [edit]See also

      a type with alignment requirement as great as any other scalar type
      (typedef)[edit]
      _Alignas(until C23)alignas(since C23) sets alignment requirements of an object
      (specifier)
      C++ documentation foralignof operator
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/_Alignof&oldid=180142"

      [8]ページ先頭

      ©2009-2025 Movatter.jp