Logarithmic and Exponential Functions in C

C provides built-in support for logarithmic and exponential functions through its math library (math.h). These functions let you calculate logarithms (both natural and base-10), exponential values, and even powers of numbers with ease.

To use these functions, you must include the math library at the top of your program:

#include <math.h>

You also need to link the math library during compilation using -lm with gcc, like so:

gcc your_program.c -o output -lm

Exponential Functions in C

The exp() function returns e raised to the power of x, where e ≈ 2.71828.

Syntax:

advertisement
double exp(double x);

Example:

#include <stdio.h>
#include <math.h>
 
int main()
{
    double testLevel = 4.0;
    double scaledScore = exp(testLevel);  // e^4
 
    printf("Exponential Score Scaling: %.4f\n", scaledScore);
    return 0;
}

Output:

Free 30-Day C++ Certification Bootcamp is Live. Join Now!
Exponential Score Scaling: 54.5982

This C program demonstrates how to calculate an exponential score using the exp() function from the <math.h> library. The function computes the value of e raised to the power of 4, given a testLevel of 4.0. The result is printed with four decimal places, showing how to scale values exponentially in C.

Logarithmic Functions in C

The log() function returns the natural logarithm (base e) of a number.

Syntax:

double log(double x);

Example:

#include <stdio.h>
#include <math.h>
 
int main()
{
    double certificationPoints = 15.0;
    double progress = log(certificationPoints);  // ln(15)
 
    printf("Log of Certification Points: %.4f\n", progress);
    return 0;
}

Output:

Log of Certification Points: 2.7081

The program calculates the natural logarithm of 15. It uses the log() function from the math library to do this. The result is stored in the progress variable. The program then prints the result with four decimal places. It includes the stdio.h and math.h libraries for input/output and math functions. Finally, it ends with return 0;, which signals the program finished successfully.

advertisement

log10() – Base-10 Logarithm

The log10() function returns the logarithm to base 10 of a number.

double log10(double x);

Example:

#include <stdio.h>
#include <math.h>
 
int main() {
    double mcqCount = 1000.0;
    double logLevel = log10(mcqCount);  // log10(1000) = 3
 
    printf("MCQ Difficulty Level (Log10): %.2f\n", logLevel);
    return 0;
}

Output:

log10(100.0) = 2.000000

The program calculates the base-10 logarithm of 1000 using the log10() function. It stores the result in logLevel and prints it with two decimal places. The program includes stdio.h and math.h libraries for input/output and math functions, then ends with return 0;.

log1p() Function

The log1p(x) function computes the natural logarithm of (1 + x), and is more accurate when x is very small (compared to log(1 + x)):

#include <stdio.h>
#include <math.h>
 
int main() {
    double result = log1p(0.000001);  // ln(1 + 0.000001)
    printf("log1p(0.000001) = %.12f\n", result);
    return 0;
}

Output:

log1p(0.000001) = 0.000001000000500000

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.