2.1 complex.h を使わずにやる場合

変数の宣言は、次のように行う。
  float _Complex a;
  double _Complex b;
  long double _Complex c;
変数 a の実部、虚部はそれぞれ __real__ a, __imag__ a で得られる。


/*
 * test-without-complex.h.c
 *    __real__, __imag__ 演算子
 *    符号を必ず表示させる %+f という書式
 */

#include <stdio.h>

int main(void)
{
  double _Complex a,b,c;

  a = 1+2i;

  printf("input Re b and Im b:");
  scanf("%lf%lf", &__real__ b, &__imag__ b);

  c = a + b;
  printf("%f%+fi\n", __real__ c, __imag__ c);
  return 0;
}

GCCには、虚数単位を表す _Complex_I がないが (直るの?直ったの?)、
#define _Complex_I (1.0iF)
のように自分で定義すれば良い。

桂田 祐史
2018-06-19