C语言中“复杂"的默认类型 [英] Default types for 'complex' in C

查看:100
本文介绍了C语言中“复杂"的默认类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我阅读的文档C99,以后将float complexdouble complexlong double complex作为复杂类型支持.但是,使用gcc -Wall -Wextra时,该代码将编译而不会发出警告.

According the docs I have read C99 and later support float complex, double complex and long double complex as complex types. However this code compiles without warning when using 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");
    }
}

  • 这是有效的C还是gcc的奇数?
  • complex matrix[a][b];给我们的是什么类型?
    • Is this valid C or is it a gcc oddity?
    • What type is complex matrix[a][b]; giving us?
    • 如果使用clang进行编译,则会得到:

      If you compile it with clang you get:

      main.c:9:5:警告:普通的'_Complex'需要类型说明符; 假设为"_Complex double"

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

      请参见 clang输出.

      gcc错误现已在此链接中报告. (不是我的.)

      gcc bug now reported at this link. (Not by me.)

      推荐答案

      如果您忽略了GCC和Clang,则假定类型为double(例如您的示例). lang产生:

      Looks like both GCC and Clang assume the type to be double if you omit it (like in your example). Clang produces:

       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),但似乎没有发出警告.

      While does GCC does the same (assuming double), it doesn't seem to warn it.

      无论编译器如何诊断,扩展到_Complexcomplex本身都不是 type ,而仅仅是 type指示符.因此,您需要指定类型.

      Regardless of compiler diagnostics, complex which expands to _Complex is not a type by itself but only a type specifier. So, you need to specify the type.

      这篇关于C语言中“复杂"的默认类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆