C tgamma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, tgamma() computes the gamma function Γ(x). For positive integers, Γ(n) = (n - 1)!. For example, tgamma(5) is 24 because 4! is 24.

You pass one double. The result is a double. Include <math.h>. The name starts with t for “true gamma,” to tell it apart from lgamma, which returns the log of the absolute gamma value.

Gamma grows very quickly. Large inputs can overflow. Non-positive integers are poles where gamma is undefined; those calls are domain errors.

Syntax

Pass the floating-point argument. Start with this call.

C
tgamma(5.0);
CallWhat it does
tgamma(5.0);Returns 24, which is 4!.

Include <math.h>. On GCC, link with -lm. Avoid non-positive integers.

Library declarations
C
double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x);

Parameters

ParameterDescription
xRequired. The double argument to the gamma function.

Return Value

Return ValueDescription
Gamma(x)The value of Γ(x) as a double.
Domain / range errorsNon-positive integers are invalid; large |x| may overflow.

C Examples

Example 1 — Basic Factorial Connection

Show that gamma at positive integers matches factorials.

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

int main(void) {
    printf("tgamma(5) = %.1f\n", tgamma(5.0));
    printf("tgamma(6) = %.1f\n", tgamma(6.0));
    return 0;
}

How It Works

  1. Include math.h. That declares tgamma.
  2. Gamma(5). Equals 4! = 24.
  3. Gamma(6). Equals 5! = 120.
  4. Mental map. Shift by one from ordinary factorial.

Example 2 — Non-Integer Argument

Gamma also works for many non-integers. A classic value is Γ(0.5) = √π.

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

int main(void) {
    double g = tgamma(0.5);
    double s = sqrt(3.141592653589793);

    printf("tgamma(0.5) = %.10f\n", g);
    printf("sqrt(pi)    = %.10f\n", s);
    return 0;
}

How It Works

  1. Call tgamma(0.5). A famous closed form.
  2. Build sqrt(pi). Use a portable pi constant.
  3. Compare prints. The digits should match closely.
  4. Beyond integers. Gamma extends factorial ideas to reals.

Example 3 — Check Gamma(x+1) = x * Gamma(x)

Verify the classic recurrence on a small non-integer.

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

int main(void) {
    double x = 3.5;
    double a = tgamma(x + 1.0);
    double b = x * tgamma(x);

    printf("Gamma(x+1) = %.10f\n", a);
    printf("x*Gamma(x) = %.10f\n", b);
    return 0;
}

How It Works

  1. Pick x. 3.5 avoids integer poles.
  2. Compute both sides. Direct gamma and the recurrence form.
  3. Compare. Floating values should agree closely.
  4. Useful identity. This relation is how gamma generalizes factorials.

Frequently Asked Questions

tgamma(x) returns the gamma function Gamma(x). For positive integers, Gamma(n) equals (n - 1)!.
x is required. It is the double argument passed to gamma.
A double equal to Gamma(x), within floating-point limits.
Include <math.h>. On GCC, link with -lm.
tgamma returns Gamma(x). lgamma returns the natural log of |Gamma(x)|, which is safer for very large magnitudes.

Next: trunc()

Truncate a floating value toward zero.

trunc() 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