Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'int16_t complex' does not work?

Why int16_t complex does not compile while int16_t on a x86 and x86_64 machine, is a typedef on short int? Following is a sample code tested with gcc 5.4 and 4.9 with both C99 and C11 standards. Compiler complains about having two or more data types in declaration specifiers.

Code:

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

int main()
{
    float complex x = I + I / 3 * I / 2;
    short int complex y = I + I / 3 * I / 2;
    int16_t complex z = I + I / 3 * I / 2;       /* Why ? */
    printf("x=(%+f,%+f)\n", creal(x), cimag(x));
    printf("y=(%+f,%+f)\n", creal(y), cimag(y));
    printf("z=(%+f,%+f)\n", creal(z), cimag(z));  /* Why ? */
    return 0;
}

Error:

In file included from ./complex.c:1:0:
./complex.c: In function ‘main’:
./complex.c:9:13: error: two or more data types in declaration specifiers
     int16_t complex z = I + I / 3 * I / 2;       /* Why ? */

Compiler command-line:

gcc-5 --std=c99 ./complex.c -o ./complex
gcc-4.9 --std=c99 ./complex.c -o ./complex
like image 510
sorush-r Avatar asked Jul 22 '26 03:07

sorush-r


1 Answers

First of all, complex integer types are a GCC extension. The C standard says (C11 6.2.5p11):

11 There are three complex types, designated as float _Complex, double _Complex, and long double _Complex .43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.

The _Complex is part of the type name, just like long. You cannot do this either:

typedef double dbl;
typedef long dbl ldbl;

i.e. to try to define a typedef for ldbl for long double when using a typedef for double! Likewise, you cannot use a typedef (such as int16_t is) when defining a complex type, use short int _Complex instead.

(And this of course applies to complex as well, as it is just a macro that expands to _Complex).