C ilogb() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, ilogb() returns the unbiased binary exponent of a double as an int. For a normal number, if x is written as significand × 2e, then ilogb(x) is that exponent e. ilogb(16.0) is 4 because 16 is 1 × 24 in that form.

You pass one floating-point value. The return type is int, not double. Include <math.h>. Related helpers: frexp() also exposes an exponent, and logb() returns a floating exponent.

Syntax

Pass the floating-point number. Start with this call.

C
ilogb(16.0);
CallWhat it does
ilogb(16.0)Returns 4, the power of two for 16.
ilogb(0.5)Returns -1, because 0.5 is 1 × 2^-1.

Include <math.h>. On GCC, link with -lm. Special inputs may return FP_ILOGB0, FP_ILOGBNAN, or INT_MAX for infinity — check your library docs for edge cases.

Library declarations
C
int ilogb(double x);
int ilogbf(float x);
int ilogbl(long double x);

Parameters

ParameterDescription
xRequired. The floating-point value whose exponent you want.

Return Value

Return ValueDescription
int exponentUnbiased exponent of x for finite non-zero values. Special macros/values apply for zero, NaN, and infinity.

C Examples

Example 1 — Read the Power of Two

See the exponent for a few simple powers of two.

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

int main(void) {
    printf("%d\n", ilogb(16.0));
    printf("%d\n", ilogb(0.5));
    printf("%d\n", ilogb(3.0));
    return 0;
}

How It Works

  1. Include math.h. Declares ilogb.
  2. 16.0. Stored as significand × 24, so the result is 4.
  3. 0.5. Half is 2-1, so the result is -1.
  4. 3.0. Between 2 and 4, so the unbiased exponent is 1.

Example 2 — Rebuild with scalbn

Peel off the exponent, then put a value back on that scale.

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

int main(void) {
    double x = 12.0;
    int e = ilogb(x);
    double y = scalbn(1.0, e);

    printf("e = %d\n", e);
    printf("y = %.1f\n", y);
    return 0;
}

How It Works

  1. Read exponent. ilogb(12) is 3 because 12 sits in [8, 16).
  2. Build 2^e. scalbn(1.0, 3) is 8.
  3. Meaning. You recovered the power-of-two bin for x.
  4. Related. frexp returns the fraction and writes the exponent through a pointer.

Example 3 — Compare Scale Without Logs

A higher ilogb means a larger magnitude class.

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

int main(void) {
    double a = 100.0;
    double b = 5.0;

    if (ilogb(a) > ilogb(b)) {
        printf("a is on a bigger scale\n");
    } else {
        printf("b is bigger or equal scale\n");
    }
    return 0;
}

How It Works

  1. Two values. 100 and 5.
  2. Compare exponents. ilogb(100) is greater than ilogb(5).
  3. Interpretation. 100 lives in a higher power-of-two bin.
  4. Careful. This is about scale, not a full numeric compare.

Frequently Asked Questions

ilogb() returns the unbiased binary exponent of a floating-point value as an int.
x is required. It is the floating-point value to examine.
An int exponent for finite non-zero x. Special values apply for zero, NaN, and infinity.
Include <math.h>. On GCC, link with -lm.
frexp() returns a fraction and writes the exponent through a pointer. ilogb() returns only the exponent as an int.

Next: isfinite()

Check whether a floating-point value is finite.

isfinite() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful