0

I am bit new to C language and I have a question in some basic principles about C. I'm on the way of implementing a custom complex library that deals with fixed point arithmetic.

We can use a library called complex.h in C. using that library we can declare and initialize complex variable as below.

complex float a = 2 + 3I;
complex float b = 5 + 2I;

complex float c = a + b;

but when we adding this two complex float, how does the compiler understand that we are going to add two complex numbers. Does the compiler understand it using its data type?

Then what is the approach that we need to follow implement a data type like complex?

I already know how to implement that using structures. But I need to know about how I make that structure to deal with a variable given in the format of a + bI.

To be more clear my question is how I deal with that 'I' character inside my Structure?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
BMTC
  • 3
  • 2
  • 1
    And yes, the compiler is tracking the types of expressions to decide how to add numbers in much the same way that it would decide how to add an `int` to a `double`. – alter_igel Mar 16 '21 at 20:00

2 Answers2

3

In C, the complex types are built into the language itself. Including <complex.h> merely lets you use the word complex to work with them; without the header you have to spell it out as _Complex.1 The complex types are built into the language, so the compiler has innate knowledge of them. You can't create similar types on your own.

In C++ (which the question was originially tagged with), std::complex from <complex> is a class. + and some other operators work with it because they are overloaded for it. There's nothing magical about this, and you can make your own classes that behave in this way. Unlike C++, C doesn't have operator overloading, so it's not possible in C.


1 This was done to preserve backwards compatibility with programs that use the word complex for other purposes. Words starting with _ followed by an uppercase letter are reserved, so it was illegal for programs to use the word _Complex for any purpose before the complex numbers were introduced into the language.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
0

The compiler knows how to handle performing arithmetic on complex types. You can then use the functions crealf and cimagf (or creal and cimag for complex double) to get each part.

complex float a = 2 + 3I; complex float b = 5 + 2I;
complex float c = a + b;

printf("result: %f%+fi\n", crealf(c), cimagf(c));

Output:

7.000000+5.000000i
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Actually I just want to know about how we define such operations if we are going to implement a custom structure to deal with complex numbers given in the format a + bI – BMTC Mar 16 '21 at 20:08
  • @BMTC Practically speaking you don't need to, as this is a standard-defined type. If you're looking to do it strictly for learning, you want something like `struct my_complex { double real, imag; };`, then you'd need to define functions to do the arithmetic. – dbush Mar 16 '21 at 20:10
  • I understood what u said. But is there a way to call that arithmetic functions by compiler when i just write complex float c = a + b; . Because complex.h library does something like that. I need to know how to do that without calling traditional function calling as complex c = __sum(a , b); – BMTC Mar 16 '21 at 20:24
  • @BMTC That would involve operator overloading, which is strictly a C++ feature. – dbush Mar 16 '21 at 20:24
  • I got the point. Thank you very much for answering. – BMTC Mar 16 '21 at 20:31