<tgmath.h> provides type-generic macros for the math functions. Writing sqrt(x) calls sqrtf for a float, sqrt for a double, sqrtl for a long double, and csqrt for a complex value.
It includes both math.h and complex.h, so one include replaces many suffixed function names. The macros choose from the argument types, not from the variable you assign to.
Remember
#include <tgmath.h>
float f = 2.0f; sqrt(f) -> sqrtf(f)
double d = 2.0; sqrt(d) -> sqrt(d)
long double l = 2.0L; sqrt(l) -> sqrtl(l)
double complex z; sqrt(z) -> csqrt(z)
integer arguments are treated as double
link with -lm
Include the header, call the plain function name, and let the argument type pick the variant.
Add #include <tgmath.h> instead of math.h.
Call the usual names: sqrt, pow, fabs, sin.
Remember the argument type decides: float uses the f variant, long double the l variant.
Integer arguments are treated as double.
Compile with -std=c11 and add -lm on GCC and Clang.
Piece
Required?
Role
#include <tgmath.h>
Yes
Includes math.h and complex.h
Argument types
Yes
Decide which function variant is called
-lm
Usually
Links the math library on GCC and Clang
Pseudocode
Pseudocode
call = sqrt(x)
if x is float -> sqrtf(x)
if x is double or int -> sqrt(x)
if x is long double -> sqrtl(x)
if x is complex -> csqrt(x)
with several arguments: apply the usual arithmetic conversions first
Reference
Cheat Sheet
Common type-generic macros. Each selects the f, l, or complex variant automatically.
Name
Meaning
sqrt, cbrt, pow, hypot
Roots and powers
sin, cos, tan, asin, acos, atan, atan2
Trigonometry
exp, log, log10, log2
Exponentials and logarithms
fabs, fmax, fmin, fmod
Magnitude and selection
ceil, floor, round, trunc
Rounding
creal, cimag, conj, carg
Complex-only macros that are also type-generic
Integer arguments
Treated as double
Trace
Worked Walkthrough — Which Function Is Called?
Trace how sqrt resolves for four argument types (sizes shown for x86-64 Linux).
Calculation
float f = 2.0f;
sqrt(f) -> selects sqrtf -> float result (sizeof 4)
sqrt(2.0) -> selects sqrt -> double result (sizeof 8)
sqrt(2.0L) -> selects sqrtl -> long double result (sizeof 16)
double complex z = -4.0 + 0.0 * I;
sqrt(z) -> selects csqrt -> 0.0 + 2.0i
Code
C Examples
Compile with gcc file.c -std=c11 -o out (add -lm at the end).
Example 1 — See Which Variant Is Chosen
Use sizeof on the result to show that each argument type picks a different function.
C
#include <stdio.h>
#include <tgmath.h>
int main(void) {
float f = 2.0f;
double d = 2.0;
long double ld = 2.0L;
printf("sizeof(sqrt(f)) = %zu\n", sizeof(sqrt(f)));
printf("sizeof(sqrt(d)) = %zu\n", sizeof(sqrt(d)));
printf("sizeof(sqrt(ld)) = %zu\n", sizeof(sqrt(ld)));
return 0;
}
Double argument.fabs(-3.5) selects the double version.
Integer arguments.pow(2, 10) treats both integers as double, giving 1024.
Mixed arguments.fmax(3, 7.5) converts to double by the usual arithmetic conversions.
Example 3 — Take a Square Root of a Complex Value
The same sqrt call dispatches to csqrt for a complex argument.
C
#include <stdio.h>
#include <tgmath.h>
int main(void) {
double complex z = -4.0 + 0.0 * I;
double complex r = sqrt(z);
printf("sqrt(-4+0i) = %.1f%+.1fi\n", creal(r), cimag(r));
return 0;
}
Output
sqrt(-4+0i) = 0.0+2.0i
How It Works
Complex argument.tgmath.h includes complex.h, so complex and I are available.
Automatic dispatch.sqrt(z) calls csqrt because z is complex.
Principal root. The square root of −4 is 2i.
Edge Cases & Pitfalls
Type-generic macros hide the function name, which causes a few surprises.
type
The argument type decides
float x = sqrt(2.0); still calls the double version because 2.0 is a double. The result is converted afterwards.
macro
They are macros
You cannot take the address of sqrt from this header as a plain function.
int
Integers become double
sqrt(16) uses the double version. Cast to float if you want sqrtf.
support
Not for C++
C++ gets overloads from <cmath>; tgmath.h there simply includes that header.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
Dispatch
Resolved at compile time; no run-time cost
Any math code
float variants
Smaller and often faster in bulk
Graphics and signal processing
long double variants
Slower on many CPUs
Extra precision
GCC and Clang implement the macros directly. Recent MSVC versions ship tgmath.h for real arguments, but complex arguments are not supported because MSVC lacks C99 _Complex.
Remember
Key Takeaways
Role: one name for every precision: sqrt, pow, sin, and more.
Dispatch: the argument type picks f, l, or complex variants.
Includes:math.h and complex.h come along automatically.
Link: add -lm on GCC and Clang.
One line:tgmath.h lets you call sqrt once and have C choose the right precision or the complex version for you.
Frequently Asked Questions
tgmath.h is a C99 header of type-generic macros. It maps calls like sqrt(x) to sqrtf, sqrt, sqrtl, or csqrt depending on the type of x.
They look at the types of the arguments after the usual arithmetic conversions. Integer arguments are treated as double.
Yes. It includes both, so you rarely need to include them separately.
Yes. The explicit names from math.h remain available when you want a specific variant.
Usually yes on GCC and Clang, because the math functions live in libm.
Recent MSVC versions provide it for real arguments only. Complex arguments are not supported because MSVC does not implement C99 _Complex.