Author Topic: HELP!!!! Need help with <complex.h>!!!  (Read 6250 times)

Xian82

  • Guest
HELP!!!! Need help with <complex.h>!!!
« on: November 12, 2009, 11:10:07 pm »
Hi there,

Two things:

1)I'm new to this forum so hi all.

2)I have an issue with <complex.h>, I cannot declare variables using this library. I'm coding in C and using Code::Blocks and I've literally copied and pasted sample code from online and found that it will not compile for me. How does one declare a variable using <complex.h>?  Is this a common issue with Code::Blocks? Here's some sample code that doesn't work for me:

#include <complex.h>
#include <stdio.h>

int main(void)
{
complex c=2+3*I;

printf("%g + %gi\n", creal(c), cimag(c));
return 0;
}


However this compiles but when I try to assign a value to the variable it no longer works:

#include <complex.h>
#include <stdio.h>

int main(void)
{
_complex c;

return 0;
}

Can anyone help me out with this?

Online stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: HELP!!!! Need help with <complex.h>!!!
« Reply #1 on: November 12, 2009, 11:56:42 pm »
Code::Blocks is an IDE; it is NOT a compiler!!

The website does not support learning how to program in C/C++ or any other language.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: HELP!!!! Need help with <complex.h>!!!
« Reply #2 on: November 14, 2009, 12:23:38 am »
Even if the forum is about an IDE, I woud try to guess what is wrong  :) First of all, you cannot compile such things in C, you need a C++ compiler, therefore, a C++ project.

(1) The first program will not compile in C. That's because you need classes and custom defined operators '+' and '*' for it. Nothing like that is supported in C. The statement "complex c = 2 + 3*I;" is an error in C.
(2) The second program can compile in C, providing you have defined a structure "_complex". Usage of that structure is the same as usage of any other structure:

c.re = 2;
c.im = 3;

but this is not the thing you have wanted most likely. Therefore, switch to C++ and make sure you are writing a C++ project.