C log10() Function

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

Definition

In C, log10() returns the base-10 logarithm of x. log10(100) is 2, and log10(1) is 0.

You pass one double, and it must be greater than 0. The result is a double. Include <math.h>.

Syntax

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

C
log10(100.0);
CallWhat it does
log10(100.0)Returns 2, because 10² is 100.

Include <math.h>. On GCC, link with -lm. For the natural log, use log().

Library declarations
C
double log10(double x);
float log10f(float x);
long double log10l(long double x);

Parameters

ParameterDescription
xRequired. A double greater than 0.

Return Value

Return ValueDescription
Base-10 logA double. log10(100) is 2.
NaNReturned when x is negative.

C Examples

Example 1 — Basic Common Logs

Landmark powers of ten return clean integers.

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

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

How It Works

  1. Include math.h. That declares log10.
  2. log10(1) is 0. Because 10^0 = 1.
  3. log10(10) is 1. One power of ten.
  4. log10(1000) is 3. Three powers of ten (10^3).

Example 2 — Count Digits of a Positive Integer

For an integer n > 0, the digit count is floor(log10(n)) + 1.

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

int main(void) {
    int n = 12345;
    int digits = (int)floor(log10((double)n)) + 1;

    printf("n = %d\n", n);
    printf("digits = %d\n", digits);
    return 0;
}

How It Works

  1. Convert to double. log10 needs a floating argument.
  2. Take log10. log10(12345) is a bit over 4.
  3. Floor then +1. floor gives 4; add 1 for five digits.
  4. Only for n > 0. Zero and negatives need separate handling.

Example 3 — Inverse With pow()

Raising 10 to log10(x) recovers x (within floating-point error).

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

int main(void) {
    double x = 250.0;
    double t = log10(x);
    double back = pow(10.0, t);

    printf("x            = %.6f\n", x);
    printf("log10(x)     = %.6f\n", t);
    printf("pow(10, t)   = %.6f\n", back);
    return 0;
}

How It Works

  1. Find the exponent. log10(250) is about 2.398.
  2. Raise 10 again. pow(10.0, t) rebuilds the original scale.
  3. Round-trip. You get back ~250.
  4. Pair for base 10. Use log10 + pow(10, …), not exp/log, when thinking in decades.

Frequently Asked Questions

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

Next: modf()

Split a floating value into integer and fractional parts.

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