The header <tgmath.h>
includes the headers <math.h>
and <complex.h>
and defines several typegeneric macros that determine which real or, when applicable, complex function to call based on the types of the arguments.
For each macro, the parameters whose corresponding real type in the unsuffixed math.h function is double are known as generic parameters (for example, both parameters of pow are generic parameters, but only the first parameter of scalbn is a generic parameter)
When a <tgmath.h>
macro is used the types of the arguments passed to the generic parameters determine which function is selected by the macro as described below. If the types of the arguments are not compatible with the parameter types of the selected function, the behavior is undefined (e.g. if a complex argument is passed into a realonly tgmath macro: float complex fc; ceil(fc) or double complex dc; double d; fmax(dc, d) are examples of undefined behavior)
Note: typegeneric macros were implemented in implementationdefined manner in C99, but C11 keyword _Generic makes it possible to implement these macros in portable manner.
[edit] Complex/real typegeneric macros
For all functions that have both real and complex counterparts, a typegeneric macro XXX
exists, which calls either of:

 float variant
XXXf
 double variant
XXX
 long double variant
XXXl

 float variant
cXXXf
 double variant
cXXX
 long double variant
cXXXl
An exception to the above rule is the fabs
macro (see the table below).
The function to call is determined as follows:
 If any of the arguments for the generic parameters is imaginary, the behavior is specified on each function reference page individually (in particular, sin, cos, tag, cosh, sinh, tanh, asin, atan, asinh, and atanh call real functions, the return types of sin, tan, sinh, tanh, asin, atan, asinh, and atanh are imaginary, and the return types of cos and cosh are real)
 If any of the arguments for the generic parameters is complex, then the complex function is called, otherwise the real function is called.
 If any of the arguments for the generic parameters is long double, then the long double variant is called. Otherwise, if any of the parameters is double or integer, then the double variant is called. Otherwise, float variant is called.
The typegeneric macros are as follows:
[edit] Realonly functions
For all functions that do not have complex counterparts, with the exception of modf
, a typegeneric macro XXX
exists, which calls either of the variants of a real function:
 float variant
XXXf
 double variant
XXX
 long double variant
XXXl
The function to call is determined as follows:
 If any of the arguments for the generic parameters is long double, then the long double variant is called. Otherwise, if any of the arguments for the generic parameters is double, then the double variant is called. Otherwise, float variant is called.
[edit] Complexonly functions
For all complex number functions that do not have real counterparts, a typegeneric macro cXXX
exists, which calls either of the variants of a complex function:
The function to call is determined as follows:
 If any of the arguments for the generic parameters is real, complex, or imaginary, then the appropriate complex function is called.
[edit] Example
#include <stdio.h>
#include <tgmath.h>
int main(void)
{
int i = 2;
printf("sqrt(2) = %f\n", sqrt(i)); // argument type is int, calls sqrt
float f = 0.5;
printf("sin(0.5f) = %f\n", sin(f)); // argument type is float, calls sinf
float complex dc = 1 + 0.5*I;
float complex z = sqrt(dc); // argument type is float complex, calls csqrtf
printf("sqrt(1 + 0.5i) = %f+%fi\n",
creal(z), // argument type is float complex, calls crealf
cimag(z)); // argument type is float complex, calls cimagf
}
Output:
sqrt(2) = 1.414214
sin(0.5f) = 0.479426
sqrt(1 + 0.5i) = 1.029086+0.242934i
[edit] References
 C11 standard (ISO/IEC 9899:2011):

 7.25 Typegeneric math <tgmath.h> (p: 373375)
 C99 standard (ISO/IEC 9899:1999):

 7.22 Typegeneric math <tgmath.h> (p: 335337)