C 中的复数语法错误(使用 VScode)

Complex number syntax error in C (using VScode)

我想在 c 中使用复数(使用 VScode),但它不起作用。

其他没有复数的c代码可以编译&执行成功,

我已经包含 complex.h 并将 Cstandard 设置为 C99

这是我的代码和错误消息

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

int main() 
{
    double complex z1 = 1.0 + 3.0 * I;
    double complex z2 = 1.0 - 4.0 * I;

    printf(" values: Z1 = %.2f + %.2fi\tZ2 = %.2f + %.2fi\n", 
           creal(z1), 
           cimag(z1), 
           creal(z2), 
           cimag(z2));

    getchar();
}

错误信息:

c:\grchen\C\code\practice\dft.c: In function 'int main()':
c:\grchen\C\code\practice\dft.c:6:20: error: expected initializer before 'z1'
     double complex z1 = 1.0 + 3.0 * I;
                    ^~
c:\grchen\C\code\practice\dft.c:7:20: error: expected initializer before 'z2'
     double complex z2 = 1.0 - 4.0 * I;
                    ^~
c:\grchen\C\code\practice\dft.c:12:18: error: 'z1' was not declared in this scope
            creal(z1),
                  ^~
c:\grchen\C\code\practice\dft.c:12:18: note: suggested alternative: 'y1'
            creal(z1),
                  ^~
                  y1
c:\grchen\C\code\practice\dft.c:14:18: error: 'z2' was not declared in this scope
            creal(z2),
                  ^~
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

> Executing task: g++ -g c:\grchen\C\code\practice\dft.c -o C:\grchen\C\code\dft.exe <

c:\grchen\C\code\practice\dft.c: In function 'int main()':
c:\grchen\C\code\practice\dft.c:6:20: error: expected initializer before 'z1'
     double complex z1 = 1.0 + 3.0 * I;
                    ^~
c:\grchen\C\code\practice\dft.c:7:20: error: expected initializer before 'z2'
     double complex z2 = 1.0 - 4.0 * I;
                    ^~
c:\grchen\C\code\practice\dft.c:12:18: error: 'z1' was not declared in this scope
            creal(z1),
                  ^~
c:\grchen\C\code\practice\dft.c:12:18: note: suggested alternative: 'y1'
            creal(z1),
                  ^~
                  y1
c:\grchen\C\code\practice\dft.c:14:18: error: 'z2' was not declared in this scope
            creal(z2),

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/i686-w64-mingw32",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tr1",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/include",
                "${workspaceFolder}/**"
            ],
            "cStandard": "c99",
            "cppStandard": "c++98",
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/i686-w64-mingw32",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/tr1",
                "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/include",
                "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }

        }
    ],
    "version": 4
}

code&error properties

感谢您的宝贵时间。

(我不是母语,我的英语很基础,请不要介意)

您的问题是您将其编译为 C++ 代码,而不是 C11。

我能够在 Windows 10、GCC v8.1、x64 上使用 mingw64 编译它。

> gcc -std=c11 a.c

工作并生成按预期运行的 .exe

> g++ a.c

产生如您所说的编译错误。编译成C11程序应该没问题