1

I have a code that uses complex number.

When I declare the variable "x", which is a complex number with size of double, should I declare it as:

complex double x;

or

double complex x;

?
Does it make any difference? Does anyone have experience on compilers not understanding either of them?

mindriot
  • 5,413
  • 1
  • 25
  • 34
Tom
  • 758
  • 1
  • 6
  • 22
  • https://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c – arc_lupus Jun 05 '16 at 06:45
  • 1
    It probably makes no difference because all "typey words" at the beginning of a declaration can be reordered: `static const unsigned long int` is the same as `int unsigned static long const`, so I'd assume `_Complex` (and its macro alias `complex`) follow the same rule. – melpomene Jun 05 '16 at 06:51
  • I don't know why this reasonable question is being downvoted. – Keith Thompson Jun 06 '16 at 15:38

2 Answers2

4

The C standard says, in 6.7.2 Type Specifiers

Each list of type specifiers shall be one of the following sets [...] the type specifiers may occur in any order

  • ...
  • float _Complex
  • double _Complex
  • long double _Complex

Although the standard consistently puts _Complex (and therefore complex) after double, float, and long double, it permits any order

#include <complex.h>

int main(void)
{
    long double complex d1;
    double long complex d2;
    complex long double d3;
    complex double long d4;
    double complex long d5;
    long complex double d6;
}

demo: http://coliru.stacked-crooked.com/a/5cbc966b0c1b096e

Cubbi
  • 46,567
  • 13
  • 103
  • 169
2

This is specified in the C Standard (N1570 draft), section 6.7.2.

A type-specifier is any of several keywords and other constructs, including float, long, double, and _Complex. Paragraph 2 says (emphasis added)

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

And in paragraph 5:

Each of the comma-separated multisets designates the same type, except that [irrelevant exception omitted].

The listed multisets include:

  • float _Complex
  • double _Complex
  • long double _Complex

Which means that, for example, long double _Complex and _Complex double long are both equally valid, and have the same meaning.

(The standard uses the slightly obscure term "multiset" because the number of times a type specifier occurs can be meaningful. If not for long long, it would have used the word "set".)

And the <complex.h> standard header defines the macro complex, which expands to the keyword _Complex.

I know of no C compiler that supports _Complex but doesn't permit type specifiers to be in any order. If you find one, submit a bug report.

Having said that, there's no point in arbitrarily reordering type specifiers. double complex is the most idiomatic form (which of course requires #include <complex.h>). Though the syntax doesn't treat them that way, you can think of double as an adjective and complex as a noun; double complex is a complex type, not a double type.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631