summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fsizeof.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Fsizeof.html
new repository
Diffstat (limited to 'devdocs/c/language%2Fsizeof.html')
-rw-r--r--devdocs/c/language%2Fsizeof.html69
1 files changed, 69 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fsizeof.html b/devdocs/c/language%2Fsizeof.html
new file mode 100644
index 00000000..afac1a41
--- /dev/null
+++ b/devdocs/c/language%2Fsizeof.html
@@ -0,0 +1,69 @@
+ <h1 id="firstHeading" class="firstHeading">sizeof operator</h1> <p>Queries size of the object or type.</p>
+<p>Used when actual size of the object must be known.</p>
+<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>sizeof(</code> <span class="t-spar">type</span> <code>)</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <code>sizeof</code> <span class="t-spar">expression</span> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>Both versions return a value of type <code><a href="../types/size_t" title="c/types/size t">size_t</a></code>.</p>
+<h3 id="Explanation"> Explanation</h3> <div class="t-li1">
+<span class="t-li">1)</span> Returns the size, in bytes, of the <a href="object#Object_representation" title="c/language/object">object representation</a> of <span class="t-spar">type</span>
+</div> <div class="t-li1">
+<span class="t-li">2)</span> Returns the size, in bytes, of the object representation of the type of <span class="t-spar">expression</span>. No implicit conversions are applied to <span class="t-spar">expression</span>.</div> <h3 id="Notes"> Notes</h3> <p>Depending on the computer architecture, a <a href="https://en.wikipedia.org/wiki/byte" class="extiw" title="enwiki:byte">byte</a> may consist of 8 or more bits, the exact number provided as <code><a href="../types/limits" title="c/types/limits">CHAR_BIT</a></code>.</p>
+<p><code>sizeof(char)</code>, <code>sizeof(signed char)</code>, and <code>sizeof(unsigned char)</code> always return <code>1</code>.</p>
+<p>sizeof cannot be used with function types, incomplete types (including <code>void</code>), or <a href="bit_field" title="c/language/bit field">bit-field</a> lvalues.</p>
+<p>When applied to an operand that has <a href="struct" title="c/language/struct">structure</a> or <a href="union" title="c/language/union">union</a> type, the result is the total number of bytes in such an object, including internal and trailing padding. The trailing padding is such that if the object were an element of an array, the alignment requirement of the next element of this array would be satisfied, in other words, <code>sizeof(T)</code> returns the size of an element of a <code>T[]</code> array.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>If <span class="t-spar">type</span> is a <a href="array" title="c/language/array">VLA</a> type and changing the value of its size expression would not affect the result of <code>sizeof</code>, it is unspecified whether or not the size expression is evaluated.</p>
+</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <p><span class="t-rev-inl t-since-c99"><span>Except if the type of <span class="t-spar">expression</span> is a <a href="array" title="c/language/array">VLA</a>,</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span><span class="t-spar">expression</span> is not evaluated and the <code>sizeof</code> operator may be used in an integer <a href="constant_expression" title="c/language/constant expression">constant expression</a>.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>If the type of <span class="t-spar">expression</span> is a <a href="array" title="c/language/array">variable-length array</a> type, <span class="t-spar">expression</span> is evaluated and the size of the array it evaluates to is calculated at run time.</p>
+</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <p>Number of elements in any <a href="array" title="c/language/array">array</a> <code>a</code> <span class="t-rev-inl t-since-c99"><span>including VLA</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> may be determined with the expression <code>sizeof a / sizeof a[0]</code>. Note that if <code>a</code> has pointer type (such as after array-to-pointer conversion of function parameter type adjustment), this expression would simply divide the number of bytes in a pointer type by the number of bytes in the pointed type.</p>
+<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/sizeof" title="c/keyword/sizeof"><code>sizeof</code></a></p>
+<h3 id="Example"> Example</h3> <div class="t-example">
+<p>Sample output corresponds to a platform with 64-bit pointers and 32-bit int</p>
+<div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+
+int main(void)
+{
+ short x;
+ // type argument:
+ printf("sizeof(float) = %zu\n", sizeof(float));
+ printf("sizeof(void(*)(void)) = %zu\n", sizeof(void(*)(void)));
+ printf("sizeof(char[10]) = %zu\n", sizeof(char[10]));
+// printf("sizeof(void(void)) = %zu\n", sizeof(void(void))); // Error: function type
+// printf("sizeof(char[]) = %zu\n", sizeof(char[])); // Error: incomplete type
+
+ // expression argument:
+ printf("sizeof 'a' = %zu\n", sizeof 'a'); // type of 'a' is int
+// printf("sizeof main = %zu\n", sizeof main); // Error: Function type
+ printf("sizeof &amp;main = %zu\n", sizeof &amp;main);
+ printf("sizeof \"hello\" = %zu\n", sizeof "hello"); // type is char[6]
+ printf("sizeof x = %zu\n", sizeof x); // type of x is short
+ printf("sizeof (x+1) = %zu\n", sizeof(x + 1)); // type of x+1 is int
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">sizeof(float) = 4
+sizeof(void(*)(void)) = 8
+sizeof(char[10]) = 10
+sizeof 'a' = 4
+sizeof &amp;main = 8
+sizeof "hello" = 6
+sizeof x = 2
+sizeof (x+1) = 4</pre></div> </div> <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: TBD) </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>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.5.3.4 The sizeof operator (p: 80-81) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.3.3.4 The sizeof operator </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/sizeof" title="cpp/language/sizeof">C++ documentation</a></span> for <span class=""><span><code>sizeof</code> operator</span></span> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/language/sizeof" class="_attribution-link">https://en.cppreference.com/w/c/language/sizeof</a>
+ </p>
+</div>