Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a C function of a complex argument from C++ [duplicate]

I want to call a C function of a complex argument, declared as

double _Complex cerf ( double _Complex z );

from C++. The following works:

#include<complex.h>
double _Complex z = 1.0;
cerf( z );

However, I fail to assign an imaginary part to z:

double _Complex z = 1.0 + _Complex_I * 2.0;

does not compile under g++: "unable to find numeric literal operator ‘operator""iF’". Other expressions I tried lead to other, similarly cryptic error messages.

like image 412
Joachim W Avatar asked Dec 30 '25 21:12

Joachim W


1 Answers

C++ and C is not completely compatible with each other, especially for C99 features, which may or may not present in C++, and complex number is one of them. C++ and C compatibility

If you are using GCC, you can use the flag -fext-numeric-literals to enable support for _Complex.

Working example

like image 91
SwiftMango Avatar answered Jan 01 '26 13:01

SwiftMango



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!