diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Finteger_constant.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2Finteger_constant.html')
| -rw-r--r-- | devdocs/c/language%2Finteger_constant.html | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/devdocs/c/language%2Finteger_constant.html b/devdocs/c/language%2Finteger_constant.html new file mode 100644 index 00000000..999d4816 --- /dev/null +++ b/devdocs/c/language%2Finteger_constant.html @@ -0,0 +1,135 @@ + <h1 id="firstHeading" class="firstHeading">Integer constant</h1> <p>Allows values of integer type to be used in expressions directly.</p> +<h3 id="Syntax"> Syntax</h3> <p>An integer constant is a <a href="value_category" title="c/language/value category">non-lvalue</a> expression of the form</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">decimal-constant</span> <span class="t-spar">integer-suffix</span> <span class="t-mark">(optional)</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <span class="t-spar">octal-constant</span> <span class="t-spar">integer-suffix</span> <span class="t-mark">(optional)</span> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <span class="t-spar">hex-constant</span> <span class="t-spar">integer-suffix</span> <span class="t-mark">(optional)</span> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <span class="t-spar">binary-constant</span> <span class="t-spar">integer-suffix</span> <span class="t-mark">(optional)</span> </td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> +</table> <p>where</p> +<ul> +<li> <span class="t-spar">decimal-constant</span> is a non-zero decimal digit (<code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, <code>8</code>, <code>9</code>), followed by zero or more decimal digits (<code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, <code>8</code>, <code>9</code>) </li> +<li> <span class="t-spar">octal-constant</span> is the digit zero (<code>0</code>) followed by zero or more octal digits (<code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>) </li> +<li> <span class="t-spar">hex-constant</span> is the character sequence <code>0x</code> or the character sequence <code>0X</code> followed by one or more hexadecimal digits (<code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, <code>8</code>, <code>9</code>, <code>a</code>, <code>A</code>, <code>b</code>, <code>B</code>, <code>c</code>, <code>C</code>, <code>d</code>, <code>D</code>, <code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>) </li> +<li> <span class="t-spar">binary-constant</span> is the character sequence <code>0b</code> or the character sequence <code>0B</code> followed by one or more binary digits (<code>0</code>, <code>1</code>) </li> +<li> <span class="t-spar">integer-suffix</span>, if provided, may contain one of the following (except the unsigned prefix can be combined with one of the others; if two suffixes are used they can appear in any order): </li> +<ul> +<li> <span class="t-spar">unsigned-suffix</span> (the character <code>u</code> or the character <code>U</code>) </li> +<li> <span class="t-spar">long-suffix</span> (the character <code>l</code> or the character <code>L</code>) <span class="t-rev-inl t-since-c99"><span>or the <span class="t-spar">long-long-suffix</span> (the character sequence <code>ll</code> or the character sequence <code>LL</code>)</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> </li> +<li> <span class="t-spar">bit-precise-int-suffix</span> (the character sequence <code>wb</code> or the character sequence <code>WB</code>) <span class="t-mark-rev t-since-c23">(since C23)</span> </li> +</ul> +</ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <p>Optional single quotes (<code>'</code>) may be inserted between the digits as a separator. They are ignored by the compiler.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Explanation"> Explanation</h3> <div class="t-li1"> +<span class="t-li">1)</span> Decimal integer constant (base 10, the first digit is the most significant).</div> <div class="t-li1"> +<span class="t-li">2)</span> Octal integer constant (base 8, the first digit is the most significant).</div> <div class="t-li1"> +<span class="t-li">3)</span> Hexadecimal integer constant (base 16, the first digit is the most significant, the letters <code>a</code> through <code>f</code> represent the decimal values 10 through 15).</div> <div class="t-li1"> +<span class="t-li">4)</span> Binary integer constant (base 2, the first digit is the most significant).</div> <p>The following variables are initialized to the same value:</p> +<div class="c source-c"><pre data-language="c">int d = 42; +int o = 052; +int x = 0x2a; +int X = 0X2A; +int b = 0b101010; // C23</pre></div> <p>The following variables are also initialized to the same value:</p> +<div class="c source-c"><pre data-language="c">unsigned long long l1 = 18446744073709550592ull; // C99 +unsigned long long l2 = 18'446'744'073'709'550'592llu; // C23 +unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C23 +unsigned long long l4 = 184467'440737'0'95505'92LLU; // C23</pre></div> <h3 id="The_type_of_the_integer_constant"> The type of the integer constant</h3> <p>The type of the integer constant is the first type in which the value can fit, from the list of types which depends on which numeric base and which <span class="t-spar">integer-suffix</span> was used.</p> +<table class="wikitable"> <caption> Types allowed for integer constants </caption> <tr> <th> suffix </th> <th> decimal bases </th> <th> other bases </th> +</tr> <tr> <td>no suffix </td> <td> +<code>int</code> <p><code>long int</code><br> <code>unsigned long int</code> <span class="t-mark-rev t-until-c99">(until C99)</span><br> <code>long long int</code> <span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> <td> +<code>int</code> <p><code>unsigned int</code><br> <code>long int</code><br> <code>unsigned long int</code><br> <code>long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span><br> <code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> +</tr> <tr> <td> +<code>u</code> or <code>U</code> </td> <td> +<code>unsigned int</code> <p><code>unsigned long int</code><br> <code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> <td> +<code>unsigned int</code> <p><code>unsigned long int</code><br> <code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> +</tr> <tr> <td> +<code>l</code> or <code>L</code> </td> <td> +<code>long int</code> <p><code>unsigned long int</code><span class="t-mark-rev t-until-c99">(until C99)</span><br> <code>long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> <td> +<code>long int</code> <p><code>unsigned long int</code><br> <code>long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span><br> <code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> +</tr> <tr> <td>both <code>l</code>/<code>L</code> and <code>u</code>/<code>U</code> </td> <td> +<code>unsigned long int</code> <p><code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> <td> +<code>unsigned long int</code> <p><code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> +</tr> <tr> <td> +<code>ll</code> or <code>LL</code> </td> <td> +<code>long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span> </td> <td> +<code>long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span> <p><code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span></p> +</td> +</tr> <tr> <td>both <code>ll</code>/<code>LL</code> and <code>u</code>/<code>U</code> </td> <td> +<code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span> </td> <td> +<code>unsigned long long int</code><span class="t-mark-rev t-since-c99">(since C99)</span> </td> +</tr> <tr> <td> +<code>wb</code> or <code>WB</code> </td> <td> +<code>_BitInt(N)</code> where the width N is the smallest N greater than 1 which can accommodate the value and the sign bit<span class="t-mark-rev t-since-c23">(since C23)</span> </td> <td> +<code>_BitInt(N)</code> where the width N is the smallest N greater than 1 which can accommodate the value and the sign bit<span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> <tr> <td>both <code>wb</code>/<code>WB</code> and <code>u</code>/<code>U</code> </td> <td> +<code>unsigned _BitInt(N)</code> where the width N is the smallest N greater than 0 which can accommodate the value<span class="t-mark-rev t-since-c23">(since C23)</span> </td> <td> +<code>unsigned _BitInt(N)</code> where the width N is the smallest N greater than 0 which can accommodate the value<span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> +</table> <p>If the value of the integer constant is too big to fit in any of the types allowed by suffix/base combination, <span class="t-rev-inl t-since-c23"><span>it does not have suffixes <code>wb</code>, <code>WB</code>, <code>uwb</code>, or <code>UWB</code></span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> and the compiler supports extended integer types (such as <code>__int128</code>), the constant may be given the extended integer type; otherwise, the program is ill-formed.</p> +<h3 id="Notes"> Notes</h3> <p>Letters in the integer constants are case-insensitive: <code>0xDeAdBaBeU</code> and <code>0XdeadBABEu</code> represent the same number<span class="t-rev-inl t-since-c99"><span> (one exception is the <span class="t-spar">long-long-suffix</span>, which is either <code>ll</code> or <code>LL</code>, never <code>lL</code> or <code>Ll</code>)</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>.</p> +<p>There are no negative integer constants. Expressions such as <code>-1</code> apply the <a href="operator_arithmetic" title="c/language/operator arithmetic">unary minus operator</a> to the value represented by the constant.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c99"> +<td> <p>When used in a controlling expression of <a href="../preprocessor/conditional" title="c/preprocessor/conditional"><code> #if</code></a> or <a href="../preprocessor/conditional" title="c/preprocessor/conditional"><code> #elif</code></a>, all signed integer constants act as if they have type <code><a href="../types/integer" title="c/types/integer">intmax_t</a></code> and all unsigned integer constants act as if they have type <code><a href="../types/integer" title="c/types/integer">uintmax_t</a></code>.</p> +</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td> +</tr> </table> <p>Integer constants may be used in <a href="constant_expression" title="c/language/constant expression">integer constant expressions</a>.</p> +<p>Due to <a href="translation_phases#maximal_munch" title="c/language/translation phases">maximal munch</a>, hexadecimal integer constants ending in <code>e</code> and <code>E</code>, when followed by the operators <code>+</code> or <code>-</code>, must be separated from the operator with whitespace or parentheses in the source:</p> +<div class="c source-c"><pre data-language="c">int x = 0xE+2; // error +int y = 0xa+2; // OK +int z = 0xE +2; // OK +int q = (0xE)+2; // OK</pre></div> <p>Otherwise, a single invalid preprocessing number token is formed, which causes further analysis to fail.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <inttypes.h> +#include <stdio.h> + +int main(void) +{ + printf("123 = %d\n", 123); + printf("0123 = %d\n", 0123); + printf("0x123 = %d\n", 0x123); + printf("12345678901234567890ull = %llu\n", 12345678901234567890ull); + // the type is a 64-bit type (unsigned long long or possibly unsigned long) + // even without a long suffix + printf("12345678901234567890u = %"PRIu64"\n", 12345678901234567890u ); + + // printf("%lld\n", -9223372036854775808); // Error: + // the value 9223372036854775808 cannot fit in signed long long, which + // is the biggest type allowed for unsuffixed decimal integer constant + + printf("%llu\n", -9223372036854775808ull ); + // unary minus applied to unsigned value subtracts it from 2^64, + // this gives unsigned 9223372036854775808 + + printf("%lld\n", -9223372036854775807ll - 1); + // correct way to form signed value -9223372036854775808 +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">123 = 123 +0123 = 83 +0x123 = 291 +12345678901234567890ull = 12345678901234567890 +12345678901234567890u = 12345678901234567890 +9223372036854775808 +-9223372036854775808</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 6.4.4.1 Integer constants (p: 57-60) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 6.4.4.1 Integer constants (p: 45-46) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 6.4.4.1 Integer constants (p: 62-64) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 6.4.4.1 Integer constants (p: 54-56) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 3.1.3.2 Integer constants </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/integer_literal" title="cpp/language/integer literal">C++ documentation</a></span> for <span class=""><span>Integer literal</span></span> </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/integer_constant" class="_attribution-link">https://en.cppreference.com/w/c/language/integer_constant</a> + </p> +</div> |
