C tgmath.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C99

What Is tgmath.h?

<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
UsePrefer
One name for all precisionssqrt(), pow(), sin(), …
Real-only mathmath.h
Complex valuescomplex.h or tgmath.h
Force a specific variantCall sqrtf() or sqrtl() directly

How to Use tgmath.h in C

Include the header, call the plain function name, and let the argument type pick the variant.

  1. Add #include <tgmath.h> instead of math.h.
  2. Call the usual names: sqrt, pow, fabs, sin.
  3. Remember the argument type decides: float uses the f variant, long double the l variant.
  4. Integer arguments are treated as double.
  5. Compile with -std=c11 and add -lm on GCC and Clang.
PieceRequired?Role
#include <tgmath.h>YesIncludes math.h and complex.h
Argument typesYesDecide which function variant is called
-lmUsuallyLinks 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

Cheat Sheet

Common type-generic macros. Each selects the f, l, or complex variant automatically.

NameMeaning
sqrt, cbrt, pow, hypotRoots and powers
sin, cos, tan, asin, acos, atan, atan2Trigonometry
exp, log, log10, log2Exponentials and logarithms
fabs, fmax, fmin, fmodMagnitude and selection
ceil, floor, round, truncRounding
creal, cimag, conj, cargComplex-only macros that are also type-generic
Integer argumentsTreated as double

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

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;
}

How It Works

  1. One name, three types. sqrt returns float, double, or long double based on its argument.
  2. sizeof reveals the result type. The expression is not evaluated, only its type is inspected.
  3. Sizes vary. long double is 16 bytes on x86-64 Linux but 8 bytes on MSVC and some ARM platforms.

Example 2 — Use fabs, pow, and fmax Without Suffixes

Call math functions with integer and double arguments.

C
#include <stdio.h>
#include <tgmath.h>

int main(void) {
    printf("fabs(-3.5)   = %.1f\n", fabs(-3.5));
    printf("pow(2, 10)   = %.1f\n", pow(2, 10));
    printf("fmax(3, 7.5) = %.1f\n", fmax(3, 7.5));
    return 0;
}

How It Works

  1. Double argument. fabs(-3.5) selects the double version.
  2. Integer arguments. pow(2, 10) treats both integers as double, giving 1024.
  3. 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;
}

How It Works

  1. Complex argument. tgmath.h includes complex.h, so complex and I are available.
  2. Automatic dispatch. sqrt(z) calls csqrt because z is complex.
  3. 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.

Performance & Portability Notes

ApproachCostBest for
DispatchResolved at compile time; no run-time costAny math code
float variantsSmaller and often faster in bulkGraphics and signal processing
long double variantsSlower on many CPUsExtra 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.

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.

Next: threads.h

Start threads, lock shared data, and run one-time setup with threads.h.

threads.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful