C erf() Function

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

Definition

In C, erf() returns the error function of x. It appears in probability and statistics with the normal distribution. erf(0) is 0.

You pass one double. The result is a double between -1 and 1. Include <math.h>. On GCC, link with -lm.

Syntax

Pass the argument. Start with this call.

C
erf(0.5);
CallWhat it does
erf(0.0)Returns 0.
erf(0.5)Returns a value near 0.52.

Include <math.h>. On GCC, link with -lm. For large |x|, see also erfc().

Library declarations
C
double erf(double x);
float erff(float x);
long double erfl(long double x);

Parameters

ParameterDescription
xRequired. The double argument to the error function.

Return Value

Return ValueDescription
doubleerf(x), a value in the closed range [-1, 1]. erf(0) is 0.

C Examples

Example 1 — Basic Error Function

Print erf at a few simple points.

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

int main(void) {
    printf("%.6f\n", erf(0.0));
    printf("%.6f\n", erf(0.5));
    printf("%.6f\n", erf(-0.5));
    return 0;
}

How It Works

  1. Include math.h. That declares erf.
  2. Zero input. erf(0) is exactly 0.
  3. Positive input. erf(0.5) is about 0.5205.
  4. Odd function. erf(-x) equals -erf(x).

Example 2 — Small Value Table

Loop over a few x values and print erf(x).

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

int main(void) {
    double xs[] = { 0.0, 0.25, 0.50, 1.00, 2.00 };
    int i;

    for (i = 0; i < 5; i++) {
        printf("erf(%.2f) = %.6f\n", xs[i], erf(xs[i]));
    }
    return 0;
}

How It Works

  1. Store sample points. An array holds a few x values.
  2. Call erf in a loop. Print each pair.
  3. Watch the growth. Values rise toward 1 as x grows.
  4. Use for checks. Handy when you verify library output.

Example 3 — Relate erf and erfc

erfc(x) is defined as 1 - erf(x). Compare both forms.

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

int main(void) {
    double x = 0.75;
    double a = 1.0 - erf(x);
    double b = erfc(x);

    printf("1 - erf = %.8f\n", a);
    printf("erfc    = %.8f\n", b);
    return 0;
}

How It Works

  1. Pick a mid-range x. 0.75 keeps both forms accurate.
  2. Compute 1 - erf. Manual complement.
  3. Call erfc. Library complementary error function.
  4. Compare. For moderate x they match closely.

Frequently Asked Questions

erf() returns the error function of x. erf(0) is 0, and values stay between -1 and 1.
x is required. It is a double argument to the error function.
A double in [-1, 1]. Large positive x approaches 1.
Include <math.h>. On GCC, link with -lm.
Use erfc() when you need 1 - erf(x) for large x. Subtracting from 1 can lose precision.

Next: erfc()

Compute the complementary error function 1 − erf(x).

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