C log() Function

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

Definition

In C, log() returns the natural logarithm of x, log base e. log(1) is 0.

You pass one double, and it must be greater than 0. The result is a double. Include <math.h>. For base 10, use log10().

Syntax

Pass a number greater than 0. Start with this call.

C
log(1.0);
CallWhat it does
log(1.0)Returns 0, because e⁰ is 1.

Include <math.h>. On GCC, link with -lm.

Library declarations
C
double log(double x);
float logf(float x);
long double logl(long double x);

Parameters

ParameterDescription
xRequired. A double greater than 0.

Return Value

Return ValueDescription
Natural logA double. log(1) is 0.
NaNReturned when x is negative.

C Examples

Example 1 — Basic Natural Logs

Landmark inputs return clean results; others need more digits.

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

int main(void) {
    printf("log(1)      = %.1f\n", log(1.0));
    printf("log(exp(1)) = %.1f\n", log(exp(1.0)));
    printf("log(2)      = %.6f\n", log(2.0));
    return 0;
}

How It Works

  1. Include math.h. That declares log and exp.
  2. log(1) is 0. Because e^0 = 1.
  3. log(exp(1)) is 1. exp(1) is e, and log(e) = 1.
  4. log(2) ≈ 0.693. A common constant in growth / half-life formulas.

Example 2 — log vs log10

Same input, different bases — different answers.

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

int main(void) {
    double x = 100.0;

    printf("log(100)   = %.6f\n", log(x));
    printf("log10(100) = %.1f\n", log10(x));
    return 0;
}

How It Works

  1. Same x. Both functions take 100.
  2. Natural log. log(100) asks for the power of e → ≈ 4.605.
  3. Common log. log10(100) asks for the power of 10 → exactly 2.
  4. Pick by base. Use log for e; use log10 for decades / digits.

Example 3 — Inverse With exp()

exp undoes log for positive inputs.

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

int main(void) {
    double x = 7.5;
    double t = log(x);
    double back = exp(t);

    printf("x         = %.6f\n", x);
    printf("log(x)    = %.6f\n", t);
    printf("exp(t)    = %.6f\n", back);
    return 0;
}

How It Works

  1. Find the exponent. log(7.5) is about 2.015.
  2. Raise e again. exp(t) rebuilds the original value.
  3. Round-trip. You get back ~7.5.
  4. Pair for base e. Use log + exp, not pow(10, …), for natural-log math.

Frequently Asked Questions

log() returns the natural logarithm of x. log(1) is 0.
x is required. It must be a double greater than 0.
A double. A negative x returns NaN, and log(0) goes toward negative infinity.
Include <math.h>. On GCC, link with -lm.
No. log() is base e. Use log10() for base 10.

Next: log10()

Compute the common logarithm (base 10) of a positive value.

log10() tutorial →

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