C Program to perform complex numbers operations using structure.

Complex number operations in C using structures demonstrate how to pair two related values — a real part and an imaginary part — into a single user-defined type. Complex numbers are written as a + bi, where a is the real component and b is the imaginary component (i = √−1). They appear in signal processing, electrical engineering (AC circuits use phasor notation), and computer graphics (rotations in 2D).

This program defines a struct complex and implements four operations: addition, subtraction, multiplication, and modulus (absolute value). Each operation is a separate function that takes structs by value and returns a new struct.

Complex Number Formulas

Operation Formula Example: (3+4i) op (1−2i)
Addition (a+bi) + (c+di) = (a+c) + (b+d)i (3+1) + (4−2)i = 4+2i
Subtraction (a+bi) − (c+di) = (a−c) + (b−d)i (3−1) + (4+2)i = 2+6i
Multiplication (a+bi)(c+di) = (ac−bd) + (ad+bc)i (3×1−4×−2) + (3×−2+4×1)i = 11−2i
Modulus |a+bi| = √(a² + b²) √(9+16) = 5

C Program for Complex Number Operations

/* Complex number operations in C using structures
 * Covers: addition, subtraction, multiplication, modulus
 * Compile: gcc -ansi -Wall -Wextra complex.c -o complex -lm */
#include <stdio.h>
#include <math.h>

struct complex {
    double real;
    double imag;
};

struct complex add(struct complex a, struct complex b)
{
    struct complex r;
    r.real = a.real + b.real;
    r.imag = a.imag + b.imag;
    return r;
}

struct complex subtract(struct complex a, struct complex b)
{
    struct complex r;
    r.real = a.real - b.real;
    r.imag = a.imag - b.imag;
    return r;
}

struct complex multiply(struct complex a, struct complex b)
{
    struct complex r;
    r.real = a.real * b.real - a.imag * b.imag;
    r.imag = a.real * b.imag + a.imag * b.real;
    return r;
}

double modulus(struct complex a)
{
    return sqrt(a.real * a.real + a.imag * a.imag);
}

void print_complex(struct complex c)
{
    if (c.imag >= 0)
        printf("%.2f + %.2fi", c.real, c.imag);
    else
        printf("%.2f - %.2fi", c.real, -c.imag);
}

int main(void)
{
    struct complex a, b, result;

    printf("Enter first complex number (real imag): ");
    scanf("%lf %lf", &a.real, &a.imag);

    printf("Enter second complex number (real imag): ");
    scanf("%lf %lf", &b.real, &b.imag);

    printf("
a = ");  print_complex(a);  printf("
");
    printf("b = ");    print_complex(b);  printf("

");

    result = add(a, b);
    printf("a + b = "); print_complex(result); printf("
");

    result = subtract(a, b);
    printf("a - b = "); print_complex(result); printf("
");

    result = multiply(a, b);
    printf("a * b = "); print_complex(result); printf("
");

    printf("|a|   = %.4f
", modulus(a));
    printf("|b|   = %.4f
", modulus(b));

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra complex.c -o complex -lm
./complex

The -lm flag links the math library for sqrt(). It must come after the source file on the command line.

Sample Input and Output

Enter first complex number (real imag): 3 4
Enter second complex number (real imag): 1 -2

a = 3.00 + 4.00i
b = 1.00 - 2.00i

a + b = 4.00 + 2.00i
a - b = 2.00 + 6.00i
a * b = 11.00 - 2.00i
|a|   = 5.0000
|b|   = 2.2361
Enter first complex number (real imag): 1 0
Enter second complex number (real imag): 0 1

a = 1.00 + 0.00i
b = 0.00 + 1.00i

a + b = 1.00 + 1.00i
a - b = 1.00 - 1.00i
a * b = 0.00 + 1.00i
|a|   = 1.0000
|b|   = 1.0000

(1 × i = i, confirmed by a * b = 0 + 1.00i ✓)

Code Explanation

  • struct complex — groups two doubles into a single type. Each function takes two struct arguments by value (copies) and returns a new struct. In C89, returning structs by value is standard; only large structs make this inefficient.
  • Multiplication formula — (a+bi)(c+di) = ac + adi + bci + bdi². Since i² = −1: real part = ac − bd, imaginary part = ad + bc. This is the FOIL expansion with the i² substitution.
  • print_complex() sign handling — when c.imag < 0, the minus sign is printed explicitly and -c.imag (a positive number) is passed to printf. This avoids printing “3.00 + -2.00i” and produces the correct “3.00 – 2.00i”.
  • modulus = √(real² + imag²) — this is the Pythagorean theorem applied to the complex plane. The modulus of 3+4i is √(9+16) = 5, which represents the distance from the origin to the point (3, 4).
  • scanf(“%lf”, &a.real)&a.real is the address of the real field inside the struct. scanf fills each field separately using its address.

What This Program Teaches

  • Structs as mathematical objects — a struct can represent any multi-component concept: complex numbers, 2D points, RGB colors, date/time. Grouping related data into a struct makes code self-documenting.
  • Returning structs from functions — C allows returning structs by value. Each operation function returns a fresh struct without modifying its inputs, keeping functions pure (no side effects).
  • Conditional formatting in printf — the sign-check in print_complex() shows how to control output format programmatically rather than letting printf decide.
  • -lm linker flagsqrt() is in libm, separate from the C standard library. Forgetting -lm produces a linker error (“undefined reference to sqrt”). The flag comes after the source file.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

2 comments on “C Program to perform complex numbers operations using structure.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>