summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fcast.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%2Fcast.html
new repository
Diffstat (limited to 'devdocs/c/language%2Fcast.html')
-rw-r--r--devdocs/c/language%2Fcast.html64
1 files changed, 64 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fcast.html b/devdocs/c/language%2Fcast.html
new file mode 100644
index 00000000..aae5fe1a
--- /dev/null
+++ b/devdocs/c/language%2Fcast.html
@@ -0,0 +1,64 @@
+ <h1 id="firstHeading" class="firstHeading">cast operator</h1> <p>Performs explicit type conversion</p>
+<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>(</code> <span class="t-spar">type-name</span> <code>)</code> <span class="t-spar">expression</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>where</p>
+<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">type-name</span> </td> <td> - </td> <td> either the type <code>void</code> or any <a href="type#Type_groups" title="c/language/type">scalar type</a> </td>
+</tr> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> any <a href="expressions" title="c/language/expressions">expression</a> of <a href="type#Type_groups" title="c/language/type">scalar type</a> (unless <span class="t-spar">type-name</span> is void, in which case it can be anything) </td>
+</tr>
+</table> <h3 id="Explanation"> Explanation</h3> <p>If <span class="t-spar">type-name</span> is <code>void</code>, then <span class="t-spar">expression</span> is evaluated for its side-effects and its returned value is discarded, same as when <span class="t-spar">expression</span> is used on its own, as an <a href="statements#Expression_statements" title="c/language/statements">expression statement</a>.</p>
+<p>Otherwise, if <span class="t-spar">type-name</span> is exactly the type of <span class="t-spar">expression</span>, nothing is done (except that if <span class="t-spar">expression</span> has floating type and is represented with greater range and precision than its type indicates -- see below)</p>
+<p>Otherwise, the value of <span class="t-spar">expression</span> is converted to the type named by <span class="t-spar">type-name</span>, as follows:</p>
+<p>Every <a href="conversion" title="c/language/conversion">implicit conversion as if by assignment</a> is allowed.</p>
+<p>In addition to the implicit conversions, the following conversions are allowed:</p>
+<ul>
+<li> Any integer can be cast to any pointer type. Except for the null pointer constants such as <code><a href="../types/null" title="c/types/NULL">NULL</a></code> (which <a href="conversion" title="c/language/conversion">doesn't need a cast</a>), the result is implementation-defined, may not be correctly aligned, may not point to an object of the referenced type, and may be a <a href="object" title="c/language/object">trap representation</a>. </li>
+<li> Any pointer type can be cast to any integer type. The result is implementation-defined, even for null pointer values (they do not necessarily result in the value zero). If the result cannot be represented in the target type, the behavior is undefined (unsigned integers do not implement modulo arithmetic on a cast from pointer) </li>
+<li> Any pointer to object can be cast to any other pointer to object. If the value is not correctly aligned for the target type, the behavior is undefined. Otherwise, if the value is converted back to the original type, it compares equal to the original value. If a pointer to object is cast to pointer to any character type, the result points at the lowest byte of the object and may be incremented up to sizeof the target type (in other words, can be used to examine <a href="object" title="c/language/object">object representation</a> or to make a copy via <code><a href="../string/byte/memcpy" title="c/string/byte/memcpy">memcpy</a></code> or <code><a href="../string/byte/memmove" title="c/string/byte/memmove">memmove</a></code>). </li>
+<li> Any pointer to function can be cast to a pointer to any other function type. If the resulting pointer is converted back to the original type, it compares equal to the original value. If the converted pointer is used to make a function call, the behavior is undefined (unless the function types are <a href="type#Compatible_types" title="c/language/type">compatible</a>) </li>
+<li> When casting between pointers (either object or function), if the original value is a null pointer value of its type, the result is the correct null pointer value for the target type. </li>
+</ul> <p>In any case (both when executing an implicit conversion and in the same-type cast), if <span class="t-spar">expression</span> and <span class="t-spar">type-name</span> are floating types and <span class="t-spar">expression</span> is represented with greater range and precision than its type indicates (see <code><a href="../types/limits/flt_eval_method" title="c/types/limits/FLT EVAL METHOD">FLT_EVAL_METHOD</a></code>), the range and precision are stripped off to match the target type.</p>
+<p>The <a href="value_category" title="c/language/value category">value category</a> of the cast expression is always non-lvalue.</p>
+<h3 id="Notes"> Notes</h3> <p>Because <a href="const" title="c/language/const"><code>const</code></a>, <a href="volatile" title="c/language/volatile"><code>volatile</code></a>, <a href="restrict" title="c/language/restrict"><code>restrict</code></a>, and <a href="atomic" title="c/language/atomic"><code>_Atomic</code></a> qualifiers have effect on <a href="value_category" title="c/language/value category">lvalues</a> only, a cast to a cvr-qualified or atomic type is exactly equivalent to the cast to the corresponding unqualified type.</p>
+<p>The cast to <code>void</code> is sometimes useful to silence compiler warnings about unused results.</p>
+<p>The conversions not listed here are not allowed. In particular,</p>
+<ul>
+<li> there are no conversions between pointers and floating types </li>
+<li> there are no conversions between pointers to functions and pointers to objects (including <code>void*</code>) </li>
+</ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>If the implementation provides <code><a href="../types/integer" title="c/types/integer">intptr_t</a></code> and/or <code><a href="../types/integer" title="c/types/integer">uintptr_t</a></code>, then a cast from a pointer to an object type (including <i>cv</i> <code>void</code>) to these types is always well-defined. However, this is not guaranteed for a function pointer.</p>
+</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <p>Note that conversions between function pointers and object pointers are accepted as extensions by many compilers, and expected by some usages of <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html">POSIX <code>dlsym()</code> function</a>.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+
+int main(void)
+{
+ // examining object representation is a legitimate use of cast
+ double d = 3.14;
+ printf("The double %.2f (%a) is: ", d, d);
+ for (size_t n = 0; n &lt; sizeof d; ++n)
+ printf("0x%02x ", ((unsigned char*)&amp;d)[n]);
+
+ // edge cases
+ struct S { int x; } s;
+// (struct S)s; // error; not a scalar type
+ // even though casting to the same type does nothing
+ (void)s; // okay to cast any type to void
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">The double 3.14 (0x1.91eb851eb851fp+1) is: 0x1f 0x85 0xeb 0x51 0xb8 0x1e 0x09 0x40</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C23 standard (ISO/IEC 9899:2023): </li>
+<ul><li> 6.5.4 Cast operators (p: TBD) </li></ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 6.5.4 Cast operators (p: 65-66) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 6.5.4 Cast operators (p: 91) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.5.4 Cast operators (p: 81) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.3.4 Cast operators </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/explicit_cast" title="cpp/language/explicit cast">C++ documentation</a></span> for <span class=""><span>explicit type conversion</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/cast" class="_attribution-link">https://en.cppreference.com/w/c/language/cast</a>
+ </p>
+</div>