C Math log() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The log() function computes the natural logarithm ln(x)—logarithm to base e (Euler’s number, ≈ 2.718). It is the inverse of exp() and appears in growth, decay, entropy, and machine-learning formulas.

01

ln(x)

Base e.

02

math.h

Link -lm.

03

x > 0

Domain.

04

log(1)=0

Identity.

05

exp

Inverse.

06

vs log10

Base 10.

Definition and Usage

The natural logarithm answers: “To what power must e be raised to get x?” If et = x, then log(x) = t. That is why log(exp(t)) = t.

In C, log(x) requires x > 0. log(1) = 0 because e&sup0; = 1. log(e) = 1 because e¹ = e.

💡
Beginner Tip

Do not pass zero or negatives to log(). The old suggestion to use clog() or log10() for those cases is wrong—neither accepts zero or negative reals either. Check x > 0 first.

📝 Syntax

Standard C declaration:

C
double log(double x);

Related variants

C
float logf(float x);           /* <math.h> */
long double logl(long double x); /* <math.h> */

Parameters

  • x — positive value; domain is x > 0.

Return Value

  • Natural logarithm ln(x) as double.
  • log(2.0) returns about 0.693147.
  • log(0) returns −∞; log(negative) returns NaN.

Headers and linking

  • #include <math.h>
  • Compile: gcc log.c -std=c11 -o log -lm
  • M_E gives the value of e on most platforms.

⚡ Quick Reference

CallMeaningResult (approx.)
log(1)e&sup0; = 10
log(M_E)e¹ = e1
log(2)ln(2)0.693147
log(exp(x))Inverse pairx
log10(100)Base 10 (not log)2.0
Natural
log(x)

Base e

Common
log10(x)

Base 10

Inverse
exp(t)

e^t

Solve
t = log(y)

if e^t = y

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Natural logs model continuous growth and appear throughout science and engineering.

📚 Getting Started

Compute the natural logarithm of a positive number.

Example 1 — Natural Logarithm of 2

Classic example from the reference: log(2.0).

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

int main(void) {
    double result = log(2.0);

    printf("ln(2.0) = %.6f\n", result);

    return 0;
}

How It Works

ln(2) ≈ 0.693 means e0.693 ≈ 2. This constant appears in half-life and doubling-time calculations.

Example 2 — log(1) and log(e)

Two values every student should know by heart.

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

int main(void) {
    printf("log(1)   = %.1f  (e^0 = 1)\n", log(1.0));
    printf("log(M_E) = %.1f  (e^1 = e)\n", log(M_E));

    return 0;
}

How It Works

These anchor the definition: log undoes exp. Any number to the power 0 is 1; e to the power 1 is e.

📈 Practical Patterns

Inverse of exp, solving equations, and base comparison.

Example 3 — log() and exp() Are Inverses

Verify log(exp(x)) returns the original value.

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

int main(void) {
    double values[] = { 0.0, 1.0, 2.5, -1.5 };
    size_t i;

    printf("   x     exp(x)    log(exp(x))\n");
    for (i = 0; i < 4; i++) {
        double x = values[i];
        printf("%5.1f  %8.4f  %12.4f\n", x, exp(x), log(exp(x)));
    }

    return 0;
}

How It Works

exp always produces a positive result, so log can undo it for any real input to exp.

Example 4 — Solve et = y for t

How long until an investment at continuous rate reaches a target?

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

int main(void) {
    double principal = 1000.0;
    double target = 2000.0;
    double rate = 0.05;
  /* target = principal * e^(rate * t)  =>  t = log(target/principal) / rate */
    double years = log(target / principal) / rate;

    printf("Grow $%.0f to $%.0f at %.0f%% continuous\n",
           principal, target, rate * 100);
    printf("Time needed: %.1f years\n", years);

    return 0;
}

How It Works

Rearranging continuous growth: t = ln(target/principal) / rate. ln(2) ÷ 0.05 ≈ 13.9 years to double at 5%.

Example 5 — log() vs log10() on the Same Value

Natural log (base e) vs common log (base 10).

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

int main(void) {
    double x = 100.0;

    printf("x = %.0f\n", x);
    printf("log(x)   = %.4f  (base e)\n", log(x));
    printf("log10(x) = %.4f  (base 10)\n", log10(x));

    return 0;
}

How It Works

log10(100) = 2 because 10² = 100. log(100) uses base e instead—different question, different answer.

🚀 Common Use Cases

  • Continuous growth/decay — solve for time with log.
  • Machine learning — cross-entropy and log-softmax.
  • Information theory — entropy uses natural log (nats) or log2.
  • Signal processing — decibel and log-scale conversions.
  • Inverse of exp() — undo exponential transforms.

🧠 How log() Works

1

Receive positive x

Domain: x > 0 only.

Input
2

Find exponent t

Solve et = x for t.

Compute
3

Return ln(x)

The power t as a double.

Output
=

Natural log

Undo exp: exp(log(x)) == x.

📝 Notes

  • Natural log uses base e ≈ 2.71828, not base 10.
  • Domain: x > 0 only. Zero and negatives are errors.
  • log(1) = 0; log(e) = 1.
  • Inverse of exp() for positive values.
  • For base-10 logarithm, use log10(), not log().
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

log() is implemented in the platform math library. In hot loops computing many logarithms, profile first—often the algorithm structure matters more than micro-optimizing individual log calls. Use logf when float precision suffices.

Conclusion

log() computes the natural logarithm ln(x) for positive x. It pairs with exp() as an inverse, solves continuous-growth equations, and differs from log10() in its base.

Continue with log10() for base-10 logarithms, or review exp() for the forward exponential.

💡 Best Practices

✅ Do

  • Validate x > 0 before calling log
  • Use log with exp as inverse pair
  • Use log10 when you need base 10
  • Include <math.h> and link -lm
  • Remember log(1) = 0 and log(e) = 1

❌ Don’t

  • Pass zero or negative values to log
  • Confuse log (base e) with log10
  • Assume clog fixes invalid domain (it is for complex)
  • Call log on a sum without log rules—log(a+b) ≠ log(a)+log(b)
  • Ignore NaN/INF from bad inputs

Key Takeaways

Knowledge Unlocked

Five things to remember about log()

Natural logarithm in C, explained simply.

5
Core concepts
📚 02

x > 0

Domain.

Rule
📈 03

log(1)=0

Anchor.

Identity
📄 04

exp

Inverse.

Pair
🔄 05

vs log10

Base 10.

Compare

❓ Frequently Asked Questions

log(x) returns the natural logarithm ln(x) — the logarithm to base e (Euler's number, about 2.718). It answers: what power must e be raised to, to get x? For example, log(2.0) is about 0.693.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double log(double x).
No. The domain is x > 0 only. log(0) returns negative infinity (-HUGE_VAL) and may set errno to ERANGE. log(negative) returns NaN and may set errno to EDOM. Always validate input is positive.
log(x) is the natural logarithm (base e). log10(x) is the common logarithm (base 10). log10(100) is 2.0; log(100) is about 4.605. Use log10 for 'how many digits' style base-10 thinking.
They are inverse operations. log(exp(x)) equals x for all real x. exp(log(x)) equals x when x > 0. If e^t = y, then t = log(y).
logf(x) takes float and returns float. logl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?

The constant ln(2) ≈ 0.693 appears in the rule of 72 for estimating doubling time: divide 72 by the interest rate percent for a quick approximation. Precisely, doubling time at continuous rate r is log(2) / r.

Explore C Math Functions

Continue with log10() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful