blob: 1a4785896d833dad1b7f51214c4b508595066589 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<h1 id="firstHeading" class="firstHeading">_Alignof operator <span class="t-mark-rev t-since-c11">(since C11)</span>
</h1> <p>Queries the alignment requirement of its operand type.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>_Alignof(</code> <span class="t-spar">type-name</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td>
</tr> <tr class="t-sdsc"> <td> <code>alignof(</code> <span class="t-spar">type-name</span> <code>)</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
</tr>
</table> <table class="t-rev-begin"> <tr class="t-rev t-until-c23">
<td> <p>This operator is typically used through the convenience macro <a href="../types" title="c/types"><code>alignof</code></a>, which is provided in the header <a href="../types" title="c/types"><code><stdalign.h></code></a></p>
</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td>
</tr> </table> <h3 id="Explanation"> Explanation</h3> <p>Returns the <a href="object#Alignment" title="c/language/object">alignment requirement</a> of the type <a href="type#Type_names" title="c/language/type">named</a> by <span class="t-spar">type-name</span>. If <span class="t-spar">type-name</span> is an array type, the result is the alignment requirement of the array element type. The <span class="t-spar">type-name</span> cannot be function type or an incomplete type.</p>
<p>The result is an integer constant of type <code><a href="../types/size_t" title="c/types/size t">size_t</a></code>.</p>
<p>The operand is not evaluated (so external identifiers used in the operand do not have to be defined).</p>
<p>If <span class="t-spar">type-name</span> is a <a href="array" title="c/language/array">VLA</a> type, its size expression is not evaluated.</p>
<h3 id="Notes"> Notes</h3> <p>The use of <span class="t-rev-inl t-until-c23"><span><code>_Alignof</code></span><span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span><span class="t-rev-inl t-since-c23"><span><code>alignof</code></span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> with expressions is allowed by some C compilers as a non-standard extension.</p>
<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/alignof" title="c/keyword/alignof"><code>alignof</code></a>, <a href="../keyword/_alignof" title="c/keyword/ Alignof"><code>_Alignof</code></a></p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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;}));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Alignment of char = 1
Alignment of max_align_t = 16
alignof(float[10]) = 4
alignof(struct{char c; int n;}) = 4</pre></div> </div> <h3 id="Defect_reports"> Defect reports</h3> <p>The following behavior-changing defect reports were applied retroactively to previously published C standards.</p>
<table class="dsctable"> <tr> <th>DR </th> <th>Applied to </th> <th>Behavior as published </th> <th>Correct behavior </th>
</tr> <tr> <td>
<a rel="nofollow" class="external text" href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_494">DR 494</a> </td> <td>C11 </td> <td>whether the size expression in a VLA is evaluated in <code>_Alignof</code> was unspecified </td> <td>it is unevaluated </td>
</tr>
</table> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 6.5.3.4 The sizeof and alignof operators (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.5.3.4 The sizeof and _Alignof operators (p: 64-65) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.5.3.4 The sizeof and _Alignof operators (p: 90-91) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../types/max_align_t" title="c/types/max align t"> <span class="t-lines"><span>max_align_t</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> a type with alignment requirement as great as any other scalar type <br> <span class="t-mark">(typedef)</span> </td>
</tr> <tr class="t-dsc"> <td> <a href="_alignas" title="c/language/ Alignas"><code>_Alignas</code> specifier</a><span class="t-mark-rev t-since-c11">(C11)</span> </td> <td> sets alignment requirements of an object </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/alignof" title="cpp/language/alignof">C++ documentation</a></span> for <code>alignof operator</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/language/_Alignof" class="_attribution-link">https://en.cppreference.com/w/c/language/_Alignof</a>
</p>
</div>
|