C Math log10() Function

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

What You’ll Learn

The log10() function computes the common logarithm log10(x)—the logarithm to base 10. It answers “how many times do I multiply 10 to get x?” and appears in decibels, scientific notation, and orders of magnitude.

01

Base 10

Common log.

02

math.h

Link -lm.

03

x > 0

Domain.

04

log10(10)=1

Identity.

05

pow(10,t)

Inverse.

06

vs log

Base e.

Definition and Usage

The common logarithm answers: “To what power must 10 be raised to get x?” If 10t = x, then log10(x) = t.

In C, log10(x) requires x > 0. log10(1) = 0 because 10&sup0; = 1. log10(10) = 1 because 10¹ = 10. log10(1000) = 3 because 10³ = 1000.

💡
Beginner Tip

On many calculators, the log button means base 10. In C, log() is the natural log (base e) and log10() is base 10. Do not mix them up.

📝 Syntax

Standard C declaration:

C
double log10(double x);

Related variants

C
float log10f(float x);             /* <math.h> */
long double log10l(long double x); /* <math.h> */

Parameters

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

Return Value

  • Base-10 logarithm log10(x) as double.
  • log10(1000.0) returns 3.0.
  • log10(0) returns −∞; log10(negative) returns NaN.

Headers and linking

  • #include <math.h>
  • Compile: gcc log10.c -std=c11 -o log10 -lm
  • Change of base: log10(x) = log(x) / log(10.0)

⚡ Quick Reference

CallMeaningResult
log10(1)10&sup0; = 10
log10(10)10¹ = 101
log10(100)10² = 1002
log10(1000)10³ = 10003
pow(10, log10(x))Inverse pairx (x > 0)
Common
log10(x)

Base 10

Natural
log(x)

Base e

Raise 10
pow(10, t)

10^t

Solve
t = log10(y)

if 10^t = y

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Base-10 logs match how we count digits and powers of ten in everyday science.

📚 Getting Started

Compute the base-10 logarithm of a positive number.

Example 1 — Base-10 Logarithm of 1000

Classic example from the reference: log10(1000.0).

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

int main(void) {
    double number = 1000.0;
    double result = log10(number);

    printf("Log base 10 of %.0f = %.0f\n", number, result);

    return 0;
}

How It Works

10³ = 1000, so log10(1000) = 3. Each whole-number step up is one more power of ten.

Example 2 — log10(1) and log10(10)

Two anchor values that define the base-10 scale.

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

int main(void) {
    printf("log10(1)  = %.1f  (10^0 = 1)\n", log10(1.0));
    printf("log10(10) = %.1f  (10^1 = 10)\n", log10(10.0));

    return 0;
}

How It Works

Any base to the power 0 is 1. Raising 10 to the power 1 gives 10. These are the building blocks for reading logarithmic scales.

📈 Practical Patterns

Digit counting, decibels, and comparison with natural log.

Example 3 — Estimate Digit Count with log10()

For a positive integer n, floor(log10(n)) + 1 gives the number of digits (for most values; exact powers of 10 need care).

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

int main(void) {
    double values[] = { 7.0, 42.0, 999.0, 1000.0, 12345.0 };
    size_t i;

    printf("   n       log10(n)   digits (floor+1)\n");
    for (i = 0; i < 5; i++) {
        double n = values[i];
        int digits = (int)floor(log10(n)) + 1;
        printf("%8.0f  %8.4f   %d\n", n, log10(n), digits);
    }

    return 0;
}

How It Works

log10(999) is just under 3, so floor gives 2 and +1 yields 3 digits. At exactly 1000, log10 is 3.0—use integer logic or string length for production digit counting.

Example 4 — Decibel Level from Power Ratio

Sound level in decibels uses a base-10 logarithm: dB = 10 × log10(P/Pref).

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

int main(void) {
    double power = 100.0;
    double reference = 1.0;
    double db = 10.0 * log10(power / reference);

    printf("Power ratio: %.0f : %.0f\n", power, reference);
    printf("Level: %.1f dB\n", db);

    return 0;
}

How It Works

100 is 10², so log10(100) = 2 and 10 × 2 = 20 dB. Logarithmic scales compress huge ranges into readable numbers.

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

Common log (base 10) vs natural log (base e), plus change-of-base check.

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

int main(void) {
    double x = 100.0;

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

    return 0;
}

How It Works

log10(x) = log(x) / log(10) is the change-of-base formula. Use log10 directly when you want base 10—clearer and often faster than dividing two natural logs.

🚀 Common Use Cases

  • Scientific notation — express large/small numbers as mantissa × 10exponent.
  • Decibels (dB) — compress wide power or amplitude ratios.
  • Orders of magnitude — compare scales (Richter, stellar brightness).
  • Digit estimationfloor(log10(n))+1 for positive integers.
  • Inverse of 10t — solve 10^t = x with t = log10(x).

🧠 How log10() Works

1

Receive positive x

Domain: x > 0 only.

Input
2

Find exponent t

Solve 10t = x for t.

Compute
3

Return log10(x)

The power t as a double.

Output
=

Common log

Undo powers of ten: pow(10, log10(x)) == x.

📝 Notes

  • Common log uses base 10, not base e.
  • Domain: x > 0 only. Zero and negatives are errors.
  • log10(1) = 0; log10(10) = 1; log10(100) = 2.
  • Calculator log often means base 10; C log() means natural log.
  • Change of base: log10(x) = log(x) / log(10.0).
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

log10() is implemented in the platform math library. Prefer log10(x) over log(x) / log(10.0) when you need base 10—it is clearer and may be faster. In hot loops, profile the overall algorithm first; use log10f when float precision is enough.

Conclusion

log10() computes the common logarithm log10(x) for positive x. It pairs naturally with powers of ten, appears in decibels and scientific scales, and differs from log() in its base.

Continue with modf() to split fractional parts, or review log() for natural logarithms.

💡 Best Practices

✅ Do

  • Validate x > 0 before calling log10
  • Use log10 for base-10 thinking (digits, dB, powers of 10)
  • Use log for natural-growth and exp pairs
  • Include <math.h> and link -lm
  • Remember log10(10) = 1 and log10(100) = 2

❌ Don’t

  • Pass zero or negative values to log10
  • Confuse C log() (base e) with calculator “log” (often base 10)
  • Assume log(a + b) = log(a) + log(b)—that identity is false
  • Rely on floor(log10(n))+1 alone at exact powers of 10 without tests
  • Ignore NaN/INF from bad inputs

Key Takeaways

Knowledge Unlocked

Five things to remember about log10()

Common logarithm in C, explained simply.

5
Core concepts
📚 02

x > 0

Domain.

Rule
📈 03

log10(10)=1

Anchor.

Identity
🔊 04

dB scales

Real use.

Apply
🔄 05

vs log

Base e.

Compare

❓ Frequently Asked Questions

log10(x) returns the common logarithm — the logarithm to base 10. It answers: what power must 10 be raised to, to get x? For example, log10(1000.0) is 3.0 because 10^3 = 1000.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double log10(double x).
No. The domain is x > 0 only. log10(0) returns negative infinity (-HUGE_VAL) and may set errno to ERANGE. log10(negative) returns NaN and may set errno to EDOM. Always validate input is positive.
log10(x) uses base 10 (common log). log(x) uses base e (natural log). log10(100) is 2.0; log(100) is about 4.605. Use log10 when you think in powers of ten.
They undo each other for base 10. If 10^t = x, then log10(x) = t. For positive x, pow(10.0, log10(x)) equals x. To raise 10 to a power, use pow(10.0, y) or exp(y * log(10.0)).
log10f(x) takes float and returns float. log10l(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?

The pH scale uses a base-10 logarithm: pH = −log10([H+]). Each step down in pH means ten times more hydrogen ions. That is why small pH changes represent large chemical differences.

Explore C Math Functions

Continue with modf() 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