Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C code in Visual Studio 2013 with complex.h library

http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx

C99 support added visual studio 2013, but I cant use complex.h in my "C" code.

#include <stdio.h>
#include <complex.h>
int main(void)
{
    double complex dc1 = 3 + 2 * I;
    double complex dc2 = 4 + 5 * I;
    double complex result;

    result = dc1 + dc2;
    printf(" ??? \n", result);

    return 0;
}

I get syntax errors.

Edit: Sorry for the missing part.

error C2146: syntax error : missing ';' before identifier 'dc1'
error C2065: 'dc1' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'dc2'
error C2065: 'dc2' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'result'
error C2065: 'result' : undeclared identifier
error C2065: 'result' : undeclared identifier
error C2065: 'dc1' : undeclared identifier
error C2065: 'dc2' : undeclared identifier
error C2065: 'result' : undeclared identifier           
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: identifier "result" is undefined
IntelliSense: identifier "dc1" is undefined
IntelliSense: identifier "dc2" is undefined
like image 662
Lebannen Avatar asked Apr 10 '14 14:04

Lebannen


People also ask

How to compile C program with Visual Studio Code?

Compiling C program with Visual Studio Code using Windows Compilation Compilation is a process of converting the source code into machine or object code. It is done with the help of the compiler. The compiler checks the source code for the errors, and if no error arises, then it generates the machine code.

Can I run a program in Visual C++?

If you're looking for a Microsoft Visual C++ Redistributable package so that you can run a program, see the latest supported Visual C++ downloads. Microsoft Visual C++ (MSVC) refers to the C++, C, and assembly language development tools and libraries available as part of Visual Studio on Windows.

What C99 support has been added to Visual Studio 2013?

In this blog post I want to share some information about the C99 support added to the C run-time library in Visual Studio 2013. To summarize, we added declarations and implementations for missing functions in the following headers: math.h, ctype.h, wctype.h, stdio.h, stdlib.h, and wchar.h.

How do I run code in Visual Studio code editor?

Use the shortcut Ctrl+Alt+N. Or press F1 and then select/type Run Code. Or right-click the Text Editor and then click Run Code in the editor context menu. The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.


2 Answers

In case anyone is searching a year later, try

_Dcomplex dc1 = {3.0, 2.0};

for the variable declaration.

From looking inside VS2013's "complex.h" header, it seems that Microsoft decided on their own implementation for C complex numbers. You'll have to implement your own arithmetical operators using the real() and imag() functions, i.e.:

double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};
like image 134
Tanaya Avatar answered Nov 15 '22 22:11

Tanaya


Another way is to define like:

/*_Fcomplex */  _C_float_complex a =  _FCbuild(5.0F, 1.0F);    
printf( "z = %.1f% + .1fi\n", crealf(a), cimagf(a));    

/*_Dcomplex*/ _C_double_complex b = _Cbuild(3.0, 2.0);    
printf("z = %.1f% + .1fi\n",creal(b), cimag(b));    
like image 36
Mohamad Sami Avatar answered Nov 15 '22 22:11

Mohamad Sami