C log10() Function

The log10() function in C is a standard library function that computes the common logarithm (base 10) of a given number. It is part of the C standard library (math.h). This function is useful for performing logarithmic calculations with base 10.

Table of Contents

  1. Introduction
  2. log10() Function Syntax
  3. Understanding log10() Function
  4. Examples
    • Computing the Common Logarithm of a Value
    • Using log10() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The log10() function calculates the common logarithm of a given number ( x ). The common logarithm is the logarithm to the base 10 and is widely used in science and engineering.

log10() Function Syntax

The syntax for the log10() function is as follows:

#include <math.h>
double log10(double x);

Parameters:

  • x: The value for which the common logarithm is to be computed. The value must be positive.

Returns:

  • The function returns the common logarithm of the value x.

Understanding log10() Function

The log10() function takes a positive value ( x ) as input and returns the common logarithm of that value. If the input value is negative or zero, the function will return a domain error.

Examples

Computing the Common Logarithm of a Value

To demonstrate how to use log10() to compute the common logarithm of a value, we will write a simple program.

Example

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

int main() {
    double value = 100.0;

    // Compute the common logarithm of the value
    double common_log = log10(value);

    // Print the result
    printf("Common logarithm of %.2f is: %.2f\n", value, common_log);

    return 0;
}

Output:

Common logarithm of 100.00 is: 2.00

Using log10() with User Input

This example shows how to use log10() to compute the common logarithm of a value provided by the user.

Example

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

int main() {
    double value;

    // Get user input for the value
    printf("Enter a positive value: ");
    scanf("%lf", &value);

    // Check if the input value is valid
    if (value <= 0) {
        printf("Invalid input! Please enter a positive value.\n");
        return 1;
    }

    // Compute the common logarithm of the value
    double common_log = log10(value);

    // Print the result
    printf("Common logarithm of %.2f is: %.2f\n", value, common_log);

    return 0;
}

Output (example user input "100.0"):

Enter a positive value: 100.0
Common logarithm of 100.00 is: 2.00

Real-World Use Case

Calculating pH in Chemistry

In real-world applications, the log10() function can be used to calculate the pH of a solution, which is a measure of the hydrogen ion concentration.

Example: Calculating pH of a Solution

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

int main() {
    double hydrogen_concentration, pH;

    // Get user input for the hydrogen ion concentration
    printf("Enter the hydrogen ion concentration (in moles per liter): ");
    scanf("%lf", &hydrogen_concentration);

    // Check if the input value is valid
    if (hydrogen_concentration <= 0) {
        printf("Invalid input! Please enter a positive value.\n");
        return 1;
    }

    // Calculate the pH using the common logarithm
    pH = -log10(hydrogen_concentration);

    // Print the result
    printf("The pH of the solution is: %.2f\n", pH);

    return 0;
}

Output (example user input hydrogen concentration "1e-7"):

Enter the hydrogen ion concentration (in moles per liter): 1e-7
The pH of the solution is: 7.00

Conclusion

The log10() function is essential for computing the common logarithm of a value in C. It is useful in various mathematical calculations, particularly in fields like science and engineering, where logarithmic functions with base 10 are required.

Comments