What is log2 in C?
The log2 function allows users to calculate a number’s binary logarithm.
Library
The log2 function is accessible from the math.h library.
Syntax
double log2_of_num = log2(double num)
Parameters
The function takes one
The number needs to be positive (greater than
0) as the logarithm of negative numbers (and0) are undefined.
Return value
The log2 function returns the binary logarithm of the number given as a parameter. The returned number is of datatype double.
Code
#include <stdio.h>#include <math.h>int main(){double number, log2_of_number;number = 32;log2_of_number = log2(number); //taking the common log of numberprintf("log10(%lf) = %lf\n", number, log2_of_number);// log2 of number will be printed}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved