summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fcomplex%2Fi.html
blob: d1d3f90d45cbe7766d63333311e08fe4b54582d2 (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
    <h1 id="firstHeading" class="firstHeading">I </h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;complex.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define I /* unspecified */</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>The <code>I</code> macro expands to either <code><a href="complex_i" title="c/numeric/complex/Complex I">_Complex_I</a></code> or <code><a href="imaginary_i" title="c/numeric/complex/Imaginary I">_Imaginary_I</a></code>. If the implementation does not support imaginary types, then the macro always expands to <code><a href="complex_i" title="c/numeric/complex/Complex I">_Complex_I</a></code>.</p>
<p>A program may undefine and perhaps then redefine the macro <code>I</code>.</p>
<h3 id="Notes"> Notes</h3> <p>The macro is not named <code>i</code>, which is the name of the imaginary unit in mathematics, because the name <code>i</code> was already used in many C programs, e.g. as a loop counter variable.</p>
<p>The macro <code>I</code> is often used to form complex numbers, with expressions such as <code>x + y*I</code>. If <code>I</code> is defined as <code><a href="complex_i" title="c/numeric/complex/Complex I">_Complex_I</a></code>, then such expression may create a value with imaginary component <code>+0.0</code> even when <code>y</code> is <code>-0.0</code>, which is significant for complex number functions with branch cuts. The macro <code><a href="cmplx" title="c/numeric/complex/CMPLX">CMPLX</a></code> provides a way to construct a complex number precisely.</p>
<p>GCC provides a non-portable extension that allows imaginary constants to be specified with the suffix <code>i</code> on integer literals: <code>1.0fi</code>, <code>1.0i</code>, and <code>1.0li</code> are imaginary units in GNU C. A similar approach is part of standard C++ as of C++14 (<code>1.0if</code>, <code>1.0i</code>, and <code>1.0il</code> are the imaginary units in C++)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;complex.h&gt;
 
int main(void)
{
    printf("I = %.1f%+.1fi\n", creal(I), cimag(I));
 
    double complex z1 = I * I;     // imaginary unit squared
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
 
    double complex z = 1.0 + 2.0*I; // usual way to form a complex number pre-C11
    printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">I = 0.0+1.0i
I * I = -1.0+0.0i
z = 1.0+2.0i</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.3.1/6 I (p: 188) </li>
<li> G.6/1 I (p: 537) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.3.1/4 I (p: 170) </li>
<li> G.6/1 I (p: 472) </li>
</ul>
</ul>        <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="imaginary_i" title="c/numeric/complex/Imaginary I"> <span class="t-lines"><span>_Imaginary_I</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> the imaginary unit constant i <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="complex_i" title="c/numeric/complex/Complex I"> <span class="t-lines"><span>_Complex_I</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> the complex unit constant i <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="cmplx" title="c/numeric/complex/CMPLX"> <span class="t-lines"><span>CMPLX</span><span>CMPLXF</span><span>CMPLXL</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> constructs a complex number from real and imaginary parts <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/complex/operator%22%22i" title='cpp/numeric/complex/operator""i'>C++ documentation</a></span> for <code>operator""i</code> </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/numeric/complex/I" class="_attribution-link">https://en.cppreference.com/w/c/numeric/complex/I</a>
  </p>
</div>