C Language: log function(Natural Logarithm)

C Programming allows us to perform mathematical operations through the functions defined in header file. The header file contains various methods for performing mathematical operations such as sqrt(), pow(), ceil(), floor() etc.

C Math Functions

In the C Programming Language, the log function returns the logarithm of x to the base of e.

Syntax

The syntax for the log function in the C Language is:




double log(double x);



Parameters or Arguments

A value used in the calculation of the logarithm of x to the base of e. If x is negative, the log function will return a domain error. If x is zero, the log function will return a range error.

Returns

The log function returns the logarithm of x to the base of e.

Required Header

In the C Language, the required header for the log function is:




#include <math.h>



Applies To

In the C Language, the log function can be used in the following versions:

  • ANSI/ISO 9899-1990

log Example


/* Example using log by c programming */

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

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will calculate the log of */
    value = 1.5;

    /* Calculate the log of the value */
    result = log(value);

    /* Display the result of the calculation */
    printf("The Natural Logarithm of %f is %f\n", value, result);

    return 0;
}



When compiled and run, this application will output:

The Natural Logarithm of 1.500000 is 0.405465




Instagram