The default types for 'complex' in C

According to the docs, I read C99 and later float complex, double complexand long double complexas complex types. However, this code compiles without warning when used gcc -Wall -Wextra.

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

int main() {
    int a, b, c, d;
    float re, im;

    scanf("%d %d", &a, &b);
    complex matrix[a][b]; /* <------ */

    for(c=0; c<a; c++) {
        for(d=0; d<b; d++) {
            scanf("%f%fi", &re, &im);
            matrix[c][d] = re + im * I;
        }
    }

    for(c=0; c<a; c++) {
        for(d=0; d<b; d++) {
            printf("%.2f%+.2fi ", creal(matrix[c][d]), cimag(matrix[c][d]));
        }
        printf("\n");
    }
}
  • Is it valid C or is it gcc odd?
  • What type complex matrix[a][b];gives us?

If you compile it with clang, you will get:

main.c: 9: 5: warning: plain '_Complex' requires a type specifier; assuming '_Complex double'

See clang output .


The gcc error is now reported in this link . (Not me.)

+4
source share
2 answers

, GCC, Clang double, ( ). Clang :

 warning: plain '_Complex' requires a type specifier; assuming '_Complex double'
    complex matrix[a][b];
    ^
            double
/usr/include/complex.h:39:18: note: expanded from macro 'complex'
#define complex         _Complex
                        ^

GCC ( double), , , .

, complex, _Complex, , . , .

+3

C99 (ISO 9899: 1999):

  • complex - ( <complex.h>), _Complex (7.3.1/2).

  • _Complex - ( , int, double, unsigned ..) (6.7.2/1).

6.7.2/2:

- . ( , ); , , .

  • [...]

  • float _Complex

  • double _Complex
  • long double _Complex

, _Complex, , complex float, double long double .

5.1.1.3

1 ( ), , undefined .

complex , . gcc -std=c99 -pedantic, gcc.

+4

Source: https://habr.com/ru/post/1666729/


All Articles