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

时间:2019-06-11 17:13:13

标签: c visual-studio-code complex-numbers

我想在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

感谢您的时间。

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

1 个答案:

答案 0 :(得分:2)

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

我能够在Windows 10,GCC v8.1,x64上使用mingw64对其进行编译。

> gcc -std=c11 a.c

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

> g++ a.c

产生您所说的编译错误。编译为C11程序,一切都应该很好