I
來自 cppreference.com
定義在標頭檔案 <complex.h> 中 |
||
#define I /* 未指定 */ |
(C99 起) | |
宏 I
展開為 _Complex_I 或 _Imaginary_I。如果實現不支援虛數型別,則該宏始終展開為 _Complex_I。
程式可以取消定義,然後重新定義宏 I。
目錄 |
[編輯] 注意
該宏未命名為 i(它是數學中虛數單位的名稱),因為名稱 i
已在許多 C 程式中使用,例如作為迴圈計數器變數。
宏 I 通常用於形成複數,使用諸如 x + y*I 之類的表示式。如果 I
定義為 _Complex_I,則此類表示式可能會建立虛部為 +0.0
的值,即使 y
為 -0.0
,這對於具有分支切割的複數函式很重要。宏 CMPLX 提供了一種精確構造複數的方法。
GCC 提供了一個不可移植的擴充套件,允許在整數文字上使用字尾 i
指定虛數常量:1.0fi
、1.0i
和 1.0li
是 GNU C 中的虛數單位。類似的方法作為 C++14 的標準 C++ 的一部分(1.0if
、1.0i
和 1.0il
是 C++ 中的虛數單位)。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <complex.h> 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)); }
輸出
I = 0.0+1.0i I * I = -1.0+0.0i z = 1.0+2.0i