log() in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

log() in C programming language, declared in the math.h header file, is a function that returns the natural logarithm of a passed parameter.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction

In C programming, we use the log() function to compute the natural logarithm of a number. The log() function is present in math.h library file, which contains many different mathematical functions. We need to include the <math.h> header file to use the log in C.

Syntax of log in C

Here you can see the log() function takes an argument x of type double and returns the floating-point value.

log() function with float or long double datatype can be used as follows -

Free Courses by top Scaler instructors

Parameters of log in C

log() take one variable as a parameter and compute the natural logarithm value of that variable. We can pass the different types of parameters/values to the log function in C.

We can pass decimal values like float, double, long double, etc., or we can pass the integer values.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Return Values of log in C

The log() function will return the natural logarithm of x ( i.e., the log value of x with base e), and the type of this return value is double.

Exceptions of log in C

There are two types of runtime errors that can occur due to passing the parameters of a specific range. These exceptions are handled by the function internally and give the outputs as follows -

  1. x == 0 (x equals to 0) - Runtime range error occurs and function returns -inf.
  2. x < 0 (x less than 0) - Runtime domain error occurs and function returns the nan.

Code Example

In the following coding example, we passed the double type value as 7.3 in the log() function and calculated its natural log.

The output comes out to be 1.987874.

Output

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Code Examples with Different Datatypes as Parameters

1. Integers

In the following coding example, integer value 5 is passed as a parameter and its natural log comes out as 1.609438.

Output

2. Decimals (Float and Long Double Datatype)

The following code example passes the decimal values like float and long double.

Here the logf() and logl() functions are used to calculate the natural log of float and long double respectively.

Output

Conclusion

  • log in C is used to compute the natural log of the given argument.
  • The function log() is present in math.h library.
  • Syntax of log in C is double log(double x).
  • logf() and logl() can be used for float and long double values.