✓ Link copied to clipboard
C 📅 July 12, 2026 ⏱ 6 min read

C Program to Add Two Complex Numbers by Passing Structure

C Program to Add Two Complex Numbers by Passing Structure

Working with complex numbers is pretty common in many scientific fields. In this program, we’ll add up two complex numbers. We’re using C structures to handle these numbers effectively.

You’ll input two complex numbers, each having a real and an imaginary part, and the program will compute their sum.

Prerequisites for Adding Complex Numbers in C

Before starting, make sure you're familiar with:

  • Defining and using struct in C
  • Functions and parameter passing in C
  • printf and scanf for input and output
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Add Two Complex Numbers

Here's the complete code to add two complex numbers using structures and functions.

add_complex_numbers.cC▶ Run
#include <stdio.h>

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

Complex addComplex(Complex c1, Complex c2);

int main() {
    Complex c1, c2, result;

    // Get first complex number
    printf("Enter real and imaginary part of the first complex number:\n");
    scanf("%f %f", &c1.real, &c1.imag);

    // Get second complex number
    printf("Enter real and imaginary part of the second complex number:\n");
    scanf("%f %f", &c2.real, &c2.imag);

    // Add two complex numbers
    result = addComplex(c1, c2);

    // Display result
    printf("Sum = %.1f + %.1fi\n", result.real, result.imag);

    return 0;
}

Complex addComplex(Complex c1, Complex c2) {
    Complex result;
    result.real = c1.real + c2.real;
    result.imag = c1.imag + c2.imag;
    return result;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Add Two Complex Numbers Program

When you run the program, it asks for two complex numbers and then outputs their sum.

OUTPUTtext
Enter real and imaginary part of the first complex number:
2
3
Enter real and imaginary part of the second complex number:
4
5
Sum = 6 + 8i
Output of C Program to Add Two Complex Numbers by Passing Structure to a Function
Output of C Program to Add Two Complex Numbers by Passing Structure to a Function

Algorithm to Add Two Complex Numbers in C

Adding two complex numbers involves summing up their real and imaginary parts separately. Here is the algorithm the program follows step by step.

  1. Define a structure Complex to store real and imaginary parts.
  2. Declare a function addComplex to add two complex numbers.
  3. In main(), declare variables for two complex numbers and the result.
  4. Prompt the user to enter real and imaginary parts for both complex numbers.
  5. Call addComplex() with the two complex numbers as arguments.
  6. Display the sum of the complex numbers.
flowchart TD
  A([Start]) --> B[Define structure Complex]
  B --> C[Declare addComplex function]
  C --> D[Define addComplex function]
  D --> E[Declare variables c1, c2, result]
  E --> F[/Input real and imaginary parts of c1/]
  F --> G[/Input real and imaginary parts of c2/]
  G --> H[Call addComplex with c1 and c2]
  H --> I[Display result]
  I --> J([End])
Flowchart: the algorithm step by step

How the C Program to Add Two Complex Numbers Works

Let's break down the code to understand how it works.

  • #include <stdio.h>: This is the standard input-output library for printf and scanf.
  • typedef struct Complex: Defines a Complex structure to hold real and imaginary parts.
  • Complex addComplex(Complex c1, Complex c2): Declares a function to add two complex numbers.
  • int main(): The main function where program execution starts.
  • Complex c1, c2, result: Declares variables for the two input complex numbers and their sum.
  • scanf("%f %f", &c1.real, &c1.imag): Reads the real and imaginary parts of the first complex number.
  • result = addComplex(c1, c2): Calls the function to add the two complex numbers.
  • printf("Sum = %.1f + %.1fi\n", result.real, result.imag): Displays the sum of the two complex numbers.
  • return 0: Indicates the program executed successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the result of adding two complex numbers (2 + 3i) and (4 + 5i) using this program?

🎉Correct! The correct result is 6 + 8i because you add the real parts and the imaginary parts separately.
🤔Not quite. Remember to add the real parts and imaginary parts separately.

Time and Space Complexity of the C Program to Add Two Complex Numbers

Let's discuss the time and space complexity of this program.

Time Complexity: O(1). The operations involved in adding two complex numbers are constant and do not depend on the size of the input. We simply execute a fixed number of additions, as shown in the result.real = c1.real + c2.real line.

Space Complexity: O(1). We only use a fixed amount of memory for the variables c1, c2, and result. These are three instances of the Complex struct, regardless of the input values.

⚠️

Common mistake: Not returning the result from the addComplex function can mess things up. Always make sure to return the computed value.

Key takeaway: Using structures in C makes managing complex data types way easier.

FAQs About Adding Two Complex Numbers in C

Here are some common questions students have about this topic.

To add two complex numbers, you just sum their real parts and their imaginary parts separately.
A structure in C is basically a user-defined data type that lets you group different variables under one name. Each variable inside is a member.
Using structures helps group related data — like the real and imaginary parts of a complex number — making your code cleaner and easier to manage.
Yep, you can pass structures to functions either by value or by reference, which gives you flexibility in manipulating the data.
Passing large structures by value isn’t great for memory efficiency. So, in those cases, passing by reference is the way to go.

If you found this program helpful, check out these related programs:

Conclusion

Don’t underestimate how useful structures in C can be. They really simplify working with complex data types like complex numbers. Try tweaking this program to do other operations like subtraction or multiplication. It'll really help cement your grasp on structures and functions in C.

Ready to test what you know? Check out our C programming quiz and give this code a spin in our online compiler.