The Wayback Machine - https://web.archive.org/web/20240905220431/https://www.geeksforgeeks.org/complex-h-header-file-in-c-with-examples/
Open In App

<complex.h> header file in C with Examples

Last Updated : 07 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Most of the C Programs deals with complex number operations and manipulations by using complex.h header file. This header file was added in C99 Standard.

C++ standard library has a header, which implements complex numbers as a template class, complex<T>, which is different from <complex.h> in C.

Macros associated with <complex.h>
Some of the macros of <complex.h> are shown below. The values in the left side describe the Macros in complex.h and the right side describes the expansion of those macros with the keywords (_Imaginary, _Complex) added in C99 standard.

Macro Name Expands To
complex _Complex
imaginary _Imaginary
_Complex_I (const float _Complex) i
_Imaginary_I (const float _Imaginary) i
I _Imaginary_I(_Complex_I if _Imaginary_I is absent)

Below program helps to create complex numbers.

Example 1:




// C program to show the working
// of complex.h library
  
#include <complex.h>
#include <stdio.h>
  
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z
        = CMPLX(real, imag);
    printf(
        "z = %.1f% + .1fi\n",
        creal(z), cimag(z));
}


Output:

z = 1.3+4.9i

Explanation:

  • cmplx() function creates complex number objects by taking real part and imaginary parts as parameters. This function returns the object of complex numbers.
  • creal() function returns the real part of a complex number
  • cimag() function returns the imaginary part of a complex number
  • If our real and imaginary parts are of type float we use cmplxf() function to generate complex numbers and to get real and imaginary parts we use crealf(), cimagf() functions.
  • If our real and imaginary parts are of type long double we use cmplxl() function to generate complex numbers and to get real and imaginary parts we use creall(), cimagl() functions.

Example 2: We can also create complex number objects using macro I.




// C program to create a complex
// number using macro I
  
#include <complex.h>
#include <stdio.h>
  
int main(void)
{
    double complex
        z
        = 3.2 + 4.1 * I;
  
    // Creates complex number
    // with 3.2 and 4.1 as
    // real and imaginary parts
    printf(
        "z = %.1f% + .1fi\n",
        creal(z), cimag(z));
}


Output:

z = 3.2+4.1i

Functions associated with <complex.h>
The <complex.h> header file also provides some inbuilt functions to work with the complex number. Here the word “arg” stands for complex number object.

Function Description
float cabsf(float complex arg)
double cabs(double complex arg)
long double cabsl(long double complex arg)
It returns the absolute value of complex argument
float complex cacosf(float complex arg)
double complex cacos(double complex arg)
long double complex cacosl(long double complex arg)
It returns the complex arc cosine values of complex argument
float complex cacoshf(float complex arg)
double complex cacosh(double complex arg)
long double complex cacoshl(long double complex arg)
It returns the complex arc hyperbolic cosine values of complex argument
float cargf(float complex arg)
double carg(double complex arg)
long double cargl(long double complex arg)
It returns the phase angle of complex argument (in radians).
float complex casinf(float complex arg)
double complex casin(double complex arg)
long double complex casinl(long double complex arg)
It returns the complex arc sine values of complex argument
float complex casinhf(float complex arg)
double complex casin(double complex arg)
long double complex casinl(long double complex arg)
It returns the complex arc hyperbolic sine values of complex argument
float complex catanf(float complex arg)
double complex catan(double complex arg)
long double complex catanl(long double complex arg)
It returns the complex arc tangent values of complex argument
float complex catanhf(float complex arg)
double complex catan(double complex arg)
long double complex catanl(long double complex arg)
It returns the complex arc hyperbolic tangent values of complex argument
float complex ccosf(float complex arg)
double complex ccos(double complex arg)
long double complex ccosl(long double complex arg)
It returns the complex cosine values of complex argument
float complex cexpf(float complex arg)
double complex cexp(double complex arg)
long double complex cexpl(long double complex arg)
It returns the complex value earg where e is the natural logarithm base
float crealf(float complex arg)
double creal(double complex arg)
long double creall(long double complex arg)
It returns the real part of the complex argument
float cimagf(float complex arg)
double cimag(double complex arg)
long double cimagl(long double complex arg)
It returns the imaginary part of the complex argument
float complex clogf(float complex arg)
double complex clog(double complex arg)
long double complex cimagl(long double complex arg)
It returns the imaginary part of the complex argument
float complex conjf(float complex arg)
double complex conj(double complex arg)
long double complex conjl(long double complex arg)
It returns the conjugate of complex argument
float complex cpowf(float complex a, long double complex b) It returns the complex value of ab
float complex csqrtf(float complex arg)
double complex csqrt(double complex arg)
long double complex csqrtl(long double complex arg)
It returns the complex square root of argument

Examples 3: Program to find Conjugate of a complex number.




#include <complex.h>
#include <stdio.h>
  
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z
        = CMPLX(real, imag);
    double complex
        conj_f
        = conjf(z);
    printf("z = %.1f% + .1fi\n",
           creal(conj_f),
           cimag(conj_f));
}


Output:

z = 1.3-4.9i

Examples 4: Program to find the absolute value of a complex number.




#include <complex.h>
#include <stdio.h>
  
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z
        = CMPLX(real, imag);
    printf("Absolute value = %.1f",
           cabsf(z));
}


Output:

Absolute value = 5.1

Examples 4: Program to find the phase angle of a complex number.




#include <complex.h>
#include <stdio.h>
  
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z
        = CMPLX(real, imag);
    printf(
        "Phase Angle = %.1f radians\n",
        cargf(z));
}


Output:

Phase Angle = 1.3 radians


Similar Reads

Geometry using Complex Numbers &lt;std::complex&gt; in C++ | Set 1
While solving geometric problems, it is time consuming to define the point class for specifying a point on the 2D Plane or the Euclidean Plane. So, this article specifies a faster and clever way to implement the same using complex class from STL in C++. Before implementation, it is essential to understand what are complex numbers and how they can h
5 min read
time.h header file in C with Examples
The time.h header file contains definitions of functions to get and manipulate date and time information. It describes three time-related data types. clock_t: clock_t represents the date as an integer which is a part of the calendar time. time_t: time_t represents the clock time as an integer which is a part of the calendar time. struct tm: struct
4 min read
Difference between Header file and Library
Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header files in programs. These files end with .h extensi
3 min read
Build a custom Map using Header file in C++
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. Maps are implemented by self-balancing search trees. In C++ STL it uses Red-Black Tree. Here we are going to implement a custom Map class which has an integer value as the key a
15+ min read
Why can templates only be implemented in the header file?
Templates are a powerful feature in C++ that allows for the creation of generic functions and classes. They provide a way to write code that works with different data types without sacrificing type safety. However, one notable restriction when working with templates is that their implementation is often required to be in the header file. This limit
4 min read
Comment in header file name?
What happens if there is a comment in header file name? Parsing of `#include' is slightly special because comments are not recognized within the ''. Thus, in `#include ' the `/*' does not start a comment and the directive specifies inclusion of a system header file named `x/*y'. Of course, a header file with such a name is unlikely to exist on Unix
1 min read
Print "Hello World" in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can easily achieve this by taking the advantage of C p
2 min read
How to write your own header file in C?
As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question arises, is it possible to create your own header fil
4 min read
dos.h header in C with examples
dos.h is a header file of C Language. This library has functions that are used for handling interrupts, producing sound, date and time functions, etc. It is Borland specific and works in compilers like Turbo C Compiler. Below are the functions supported by this library: delay(): The delay() function in C is used to stop the execution of the program
4 min read
Modular Exponentiation of Complex Numbers
Given four integers A, B, K, M. The task is to find (A + iB)K % M which is a complex number too. A + iB represents a complex number. Examples: Input : A = 2, B = 3, K = 4, M = 5 Output: 1 + i*0 Input : A = 7, B = 3, K = 10, M = 97 Output: 25 + i*29 Prerequisite: Modular Exponentiation Approach: An efficient approach is similar to the modular expone
7 min read
Program to determine the Quadrant of a Complex number
Given a complex number in the form of the string str, the task is to determine the quadrant of the cartesian plane in which this complex number lies. Examples: Input: str = "1 + 1i" Output: Quadrant 1 Input: str = "0 + 0i" Output: Origin Approach: The idea is to first find the real and imaginary parts of a complex number. Let's say the point is (x,
7 min read
Modulus of a Complex Number
Given a complex number z, the task is to determine the modulus of this complex number. Note: Given a complex number z = a + ib the modulus is denoted by |z| and is defined as [latex]\left | z \right | = \sqrt{a^{2}+b^{2}}[/latex] Examples: Input: z = 3 + 4i Output: 5 |z| = (32 + 42)1/2 = (9 + 16)1/2 = 5 Input: z = 6 - 8i Output: 10 Explanation: |z|
5 min read
Product of Complex Numbers using three Multiplication Operation
Given four integers a, b, c, and d which represents two complex numbers of the form (a + bi) and (c + di), the task is to find the product of the given complex numbers using only three multiplication operations.Examples: Input: a = 2, b = 3, c = 4 and d = 5 Output: -7 + 22i Explanation: Product is given by: (2 + 3i)*(4 + 5i) = 2*4 + 4*3i + 2*5i + 3
6 min read
Convert given Complex Numbers into polar form and perform all arithmetic operations
Given two Complex Numbers Z1 and Z2 in the Cartesian form, the task is to convert the given complex number into polar form and perform all the arithmetic operations ( addition, subtraction, multiplication, and division ) on them. Examples: Input: Z1 = (2, 3), Z2 = (4, 6)Output:Polar form of the first Complex Number: (3.605551275463989, 0.9827937232
15 min read
Square root of two Complex Numbers
Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number. Examples: Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 - 0.707107*i Input: A = 4, B = 0Output:The Square roots are: 2-2 Approach: The given problem can
9 min read
C Program to Add Two Complex Numbers
Complex numbers are those numbers that can be expressed in the form of "a+ib" where a and b are the real numbers and i is the imaginary part called "iota" The value of i is √-1. In this article, we are going to add two complex numbers using a C program. Example of Add Two Complex NumberInput: a = ( 2 + 3i ) b = ( 4 + 5i ) Output: Sum = ( 6 + 8i ) E
3 min read
Multiplication of two complex numbers given as strings
Given two complex numbers in the form of strings. Our task is to print the multiplication of these two complex numbers. Examples: Input : str1 = "1+1i" str2 = "1+1i" Output : "0+2i"Here, (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i or "0+2i"Input : str1 = "1+-1i" str2 = "1+-1i"Output : "0+-2i"Here, (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i or "0+-2i"Mult
12 min read
Multiply N complex numbers given as strings
Given N Complex Numbers in the form of Strings, the task is to print the multiplication of these N complex numbers. Examples: Input: N = 3, V = { 3 + 1i, 2 + 1i, 5 + -7i } Output: 10+-60i Explanation: Firstly, we will multiply (3+1i) and (2+1i) to yield 5+5i. In the next step, we will multiply 5+5i and -5+-7i to yield the final result 10+-60i. Inpu
9 min read
NetworkX : Python software package for study of complex networks
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. We can generate many types of random and classic
3 min read
Geometry using Complex Numbers in C++ | Set 2
After going through previous post, we know what exactly are complex numbers and how we can use them to simulate points in a cartesian plane. Now, we will have an insight as to how to use the complex class from STL in C++.To use the complex class from STL we use #include &lt;complex&gt; Defining Point ClassWe can define our point class by typedef co
5 min read
Find the real and imaginary part of a Complex number
Given a complex number Z, the task is to determine the real and imaginary parts of this complex number.Examples: Input: z = 3 + 4i Output: Real part: 3, Imaginary part: 4Input: z = 6 – 8i Output: Real part: 6, Imaginary part: 8 Approach: A complex number can be represented as Z = x + yi, where x is real part and y is imaginary. We will follow the b
5 min read
Program for power of a complex number in O(log n)
Given a complex number of the form x + yi and an integer n, the task is to calculate the value of this complex number raised to the power n.Examples: Input: num = 17 - 12i, n = 3Output: -2431 + i ( -8676 )Input: num = 18 - 13i, n = 8Output: 16976403601 + i ( 56580909840 ) Approach: This algorithm works in O(log n) time complexity. Let c = a + bi an
8 min read
Find the conjugate of a Complex number
Given a complex number str in the form of a string, the task is to determine the conjugate of this complex number.Examples: Input: str = "3 - 4i"Output: 3 + 4iInput: str = "6 - 5i"Output: 6 + 5iApproach: A complex number is said to be a conjugate of another complex number if only the sign of the imaginary part of the two numbers is different. If co
4 min read
Header Linked List in C
Prerequisite: Linked List, Circular Linked List A header node is a special node that is found at the beginning of the list. A list that contains this type of node, is called the header-linked list. This type of list is useful when information other than that found in each node is needed. For example, suppose there is an application in which the num
5 min read
What’s difference between header files "stdio.h" and "stdlib.h" ?
These are two important header files used in C programming. While “&lt;stdio.h&gt;” is header file for Standard Input Output, “&lt;stdlib.h&gt;” is header file for Standard Library. One easy way to differentiate these two header files is that “&lt;stdio.h&gt;” contains declaration of printf() and scanf() while “&lt;stdlib.h&gt;” contains declaratio
2 min read
accumulate() and partial_sum() in C++ STL : Numeric header
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc. This article explains accumulate() and partial_sum
4 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc. See this article for more reference: accumulate()
4 min read
What is Two Way Header Linked List?
Two-Way Header Linked List, also known as a doubly linked list with a header node, is a type of linked list that contains a special node at the beginning called the header node. This node does not contain any data but serves as a permanent head of the list. The rest of the list consists of nodes that each contain data and two links pointing to the
3 min read
C program to check whether the file is JPEG file or not
Write a C program which inputs a file as a command-line arguments and detects whether the file is JPEG(Joint Photographic Experts Group) or not. Approach: We will give an image as a command line argument while executing the code. Read the first three bytes of the given image(file). After reading the file bytes, compare it with the condition for JPE
2 min read
C program to copy contents of one file to another file
[GFGTABS] C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr1, *fptr2; char filename[100]; int c; printf(&quot;Enter the filename to open for reading: &quot;); scanf(&quot;%s&quot;, filename); // Open one file for reading fptr1 = fopen(filename, &quot;r&quot;); if (fptr1 == NULL) { printf(&quot;Cannot open fi
1 min read
Practice Tags :