IBM extension

The __real__ and __imag__ operators

XL C/C++ extends the C99 and C++ standards to support the unary operators __real__ and __imag__. These operators provide the ability to extract the real and imaginary parts of a complex type. These extensions have been implemented to ease the porting applications developed with GNU C.

Read syntax diagramSkip visual syntax diagram__real__ and __imag__ operator syntax

The var_identifier is the name of a previously declared complex variable. The __real__ operator returns the real part of the complex variable, while the __imag__ operator returns the imaginary part of the variable. If the operand of these operators is an lvalue, the resulting expression can be used in any context where lvalues are allowed. They are especially useful in initializations of complex variables, and as arguments to calls to library functions such as printf and scanf that have no format specifiers for complex types. For example:

float _Complex myvar; 
__imag__(myvar) = 2.0f; 
__real__(myvar) = 3.0f; 

initializes the imaginary part of the complex variable myvar to 2.0i and the real part to 3.0, and

printf("myvar = %f + %f * i\n", __real__(myvar), __imag__(myvar));


myvar = 2.000000 + 3.000000 * i

Related information

End of IBM extension