Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

# What is the _Imaginary keyword in C?

Khizar Hayat Saani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

We use _Imaginary to declare imaginary numbers in C.

It is an expansion of the imaginary macro in <complex.h>.

An imaginary number is written as a real number multiplied by the imaginary unit, I.

An Imaginary Number

## _Imaginary types

The following are the three _Imaginary types:

1. float _Imaginary
2. double _Imginary
3. long double _Imaginary

By including the <complex.h> header, we can replace the _Imaginary keyword with imaginary and have the same effect.

## Code

#include <complex.h>#include <stdio.h>int main(){  //declaring an imaginary number using imaginary  double imaginary z1 = 3*I;  //Arithmetic operation  z1 = 1/z1;  //Printing correct to 1dp  printf("1 / (3.0i) = %+.1fi\n", cimag(z1));  //declaring an imaginary number using _Imaginary  double _Imaginary z2 = 4*I;  //Arithmetic operation  z2 = z2 / 2;  //Printing correct to 1dp  printf("(3.0i) / 2 = %+.1fi\n", cimag(z2));  return 0;}

The above code example shows how to use imaginary numbers in both forms.

First, we declare an imaginary number. To define an imaginary number, we multiply a real number by the imaginary unit, I.

Next, an arithmetic operation is performed on the imaginary number before displaying it.

cimag() is defined in the <complex.h> header. We use this to extract the imaginary part of a complex number.

Note: According to the official documentation, “A compiler that defines __STDC_IEC_559_complex__ is not required to support imaginary numbers.” We can use the complex macro instead of the imaginary macro in lines 7 and 16 of our code to get the job done.

RELATED TAGS

c

CONTRIBUTOR

Khizar Hayat Saani 