How to add two complex numbers by passing structure to a function in C language?

In C programming, complex numbers can be represented using structures with real and imaginary parts as members. To add two complex numbers, we pass the structure variables to a user-defined function that performs the addition and returns the result.

Syntax

typedef struct complex {
    float real;
    float imag;
} complex;

complex addition(complex num1, complex num2);

Algorithm

The following steps outline the process for adding two complex numbers using structures −

Step 1: Define a structure with real and imaginary parts
Step 2: Declare structure variables for input and result
Step 3: Read real and imaginary parts of first complex number
Step 4: Read real and imaginary parts of second complex number
Step 5: Call addition function with both complex numbers
Step 6: In addition function:
        - Add real parts: result.real = num1.real + num2.real
        - Add imaginary parts: result.imag = num1.imag + num2.imag
        - Return the result structure
Step 7: Display the final result

Example

The following C program demonstrates how to add two complex numbers by passing structures to a function −

#include <stdio.h>

typedef struct complex {
    float real;
    float imag;
} complex;

complex addition(complex num1, complex num2);

int main() {
    complex num1, num2, result;
    
    printf("Enter real and imaginary parts of first complex number: ");
    scanf("%f %f", &num1.real, &num1.imag);
    
    printf("Enter real and imaginary parts of second complex number: ");
    scanf("%f %f", &num2.real, &num2.imag);
    
    result = addition(num1, num2);
    
    printf("First complex number: %.1f + %.1fi<br>", num1.real, num1.imag);
    printf("Second complex number: %.1f + %.1fi<br>", num2.real, num2.imag);
    printf("Sum: %.1f + %.1fi<br>", result.real, result.imag);
    
    return 0;
}

complex addition(complex num1, complex num2) {
    complex temp;
    temp.real = num1.real + num2.real;
    temp.imag = num1.imag + num2.imag;
    return temp;
}
Enter real and imaginary parts of first complex number: 3.5 2.1
Enter real and imaginary parts of second complex number: 1.8 4.3
First complex number: 3.5 + 2.1i
Second complex number: 1.8 + 4.3i
Sum: 5.3 + 6.4i

How It Works

  • The typedef struct complex defines a new data type with real and imag members
  • The addition() function takes two complex structure parameters by value
  • Inside the function, corresponding real and imaginary parts are added separately
  • The function returns a new complex structure containing the sum
  • Pass-by-value ensures original structures remain unchanged

Conclusion

Adding complex numbers using structures in C demonstrates how to organize related data and pass structures to functions. This approach provides a clean, modular way to perform mathematical operations on complex data types.

Updated on: 2026-03-15T13:49:29+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements