1

Possible Duplicate:
How to work with complex numbers in C?

So I have this piece of C code that compiles with errors saying that 'complex' does not name a type:

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

int main ()
{
    int B=9;
    double theta;

    double complex w;
    float x,y;

    x= 5*cos (theta) - 2;
    y= 5*sin (theta);


    double complex z=x+y*I;
    w=z+(B/z);

    for(theta=0;theta<=360;theta=+30)
    { printf ("%.2f  %.2f  %.2f  %.2f",creal(z), cimag(z),y,creal(w), cimag(w));
        printf ("/n");
    } 

    return 0;

    system ("pause");
}

I already include the <complex.h> so why is there still an error for the 'complex'. There are also other errors, but let just focus on this one first.

Community
  • 1
  • 1
Shahrul Nizam
  • 23
  • 1
  • 5
  • 2
    On my box, the error goes away when I move the declaration of `z` to after the point where `x` and `y` get their values. – Fred Foo Nov 28 '12 at 16:29
  • You are not using the header file correctly http://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c – asheeshr Nov 28 '12 at 16:30
  • it isn't about the values actually, it is about the 'complex' syntax. It didn't detect the 'complex' eventhough I already #include . – Shahrul Nizam Nov 28 '12 at 16:32
  • @AshRj I did find this answer but sorry to say that I didn't quite understand it. so I need to enable C99 support?? – Shahrul Nizam Nov 28 '12 at 16:35

2 Answers2

4

Are you using GCC as your compiler? If yes, you need to enable C99 support by using the -std=c99 or -std=gnu99 compiler flag.

Also, declare variables before you use them. Here:

double complex z=x+y*I;

neither x nor y have been declared yet. Of course you also need to initialize them. For example:

float x = 5 * cos(theta) - 2;
float y = 5 * sin(theta);
double complex z = x + y * I;
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Thanks for pointing that out. I already declare x and y as float actually. I did find that information, but I didn't actually know how to implement it. Can you guide me or give me any link? :) – Shahrul Nizam Nov 28 '12 at 16:36
  • @ShahrulNizam How do you run your code ? – asheeshr Nov 28 '12 at 16:37
  • 1
    @ShahrulNizam The point is that you're using `x` and `y` before you actually declare them. Read my answer again. – Nikos C. Nov 28 '12 at 16:38
  • @NikosC. he is asking how to use the compiler flag – asheeshr Nov 28 '12 at 16:39
  • @ShahrulNizam We need to know your development environment. Are you using Dev-C++? Or aren't you using GCC at all? (For example Visual Studio?) – Nikos C. Nov 28 '12 at 16:40
  • @AshRj : I use Dev C++. :) NikosC. : I've already edited the codes and yup I use Dev C++. – Shahrul Nizam Nov 28 '12 at 16:43
  • @ShahrulNizam In the project options, go to 'parameters', then add '-std=c99' to the 'compiler' textbox (it's been a while since I used it, so first check whether there's a proper checkbox of similar that lets you enable C99 mode.) Btw, if you're using Bloodshed Dev-C++, you should switch to Orwell Dev-C++ (orwelldevcpp.blogspot.com). Bloodshed's version is very old and not maintained anymore. – Nikos C. Nov 28 '12 at 16:47
  • thanks a lot @NikosC. I've already downloaded the orwelldevc++, and I tried to run it now using that. There isn't any error about the 'complex', but it says "expected initializer before 'w'" and "'w' is not declared in this scope." Sorry for asking a lot of questions. – Shahrul Nizam Nov 28 '12 at 16:59
  • @ShahrulNizam Make sure you set it to C, not C++ (Project -> Project Options -> Compiler -> Code Generation -> Language Standard). – Nikos C. Nov 28 '12 at 17:03
  • oho ok. Previously I just create a new source file. It got no errors now but now the output is suddenly in an infinite loop. But it's ok, at least it is running. Later i can give it back to my friend. Thanks a lot guys!! :) – Shahrul Nizam Nov 28 '12 at 17:07
1

This should work :

#include <stdio.h>
#include <complex.h>
#include <math.h>
int main ()

{
 int B=9;
 double theta;
 double complex w;
 float x = 5*cos (theta) - 2;
 float y = 5*sin (theta);
 double complex z=x+y*I;

 w=z+(B/z);

 for(theta=0;theta<=360;theta=+30)
  { printf ("%.2f  %.2f  %.2f  %.2f",creal(z), cimag(z),y,creal(w), cimag(w));
   printf ("/n");
  } 

  return 0;
 }
Joze
  • 1,285
  • 4
  • 19
  • 33