Reputation: 23
I want to use Complex number in c (using VScode),but it doesn't work.
other c code without Complex number can compile & execute successfully,
I've included complex.h and set Cstandard as C99
here's my code & error message
#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();
}
error message:
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
}
Thanks for your time.
(I'm not native speaker,my English is pretty basic, please don't mind)
Upvotes: 2
Views: 1200
Reputation: 20130
Your problem is you're compiling it as C++ code, not C11.
I was able to compiler it with mingw64 on Windows 10, GCC v8.1, x64.
> gcc -std=c11 a.c
works and produce .exe which runs as expected
> g++ a.c
produce compilation errors like you stated. Compile it as C11 program and everything should be fine
Upvotes: 2