Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are both "complex float" and "float complex" valid C?

Tags:

c

My question is simply this:

Are both "complex float" and "float complex" valid C?

Both seem to be accepted without warnings by gcc.

like image 644
graffe Avatar asked Jan 16 '17 14:01

graffe


2 Answers

complex is a macro from complex.h which expands into the type specifier _Complex. This behaves as all other type specifiers, for example int, bool, double. For all type specifiers belonging to the same "group", you can combine them in various orders. This is specified by C11 6.7.2, emphasis mine:

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.

Then follows a list of valid groups of type specifiers, where we find

  • float _Complex
  • double _Complex

Meaning that any permutation of the specifiers in the same group is fine.


To take another example, there is a group

  • unsigned long long, or unsigned long long int

Which gives us the following possible combinations:

unsigned long long x;
long unsigned long y;
long long unsigned z;

or

unsigned long long int a;
unsigned long int long b;
unsigned int long long c;
int unsigned long long d;
long unsigned long int e;
long long unsigned int f;
long long int unsigned g;
long unsigned int long h;
...

These all mean the same thing.

like image 98
Lundin Avatar answered Oct 22 '22 22:10

Lundin


Yes. In general, the order of "typey words" at the beginning of a declaration doesn't matter:

static const unsigned long int x = 42;

is the same as

long const int unsigned static x = 42;

Reference: C99, 6.7.2/2

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

(Both float and _Complex are type specifiers.)

like image 5
melpomene Avatar answered Oct 22 '22 21:10

melpomene