C signbit Macro

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

Definition

In C, signbit is a macro that tests whether the sign bit of a floating-point value is set. It returns a nonzero value when the sign bit is set, and 0 otherwise.

You pass a floating value (float, double, or long double). Include <math.h>. Unlike a simple x < 0 check, signbit can detect negative zero, which still compares equal to positive zero.

Use it when the bit-level sign matters—for example when printing, classifying zeros, or copying signs with care.

Syntax

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

C
signbit(-0.0);
CallWhat it does
signbit(-0.0);Returns nonzero because the sign bit of negative zero is set.

Include <math.h>. signbit is a type-generic macro, not a single function with one fixed signature.

Library declarations
C
int signbit(real-floating x);

Parameters

ParameterDescription
xRequired. A floating value whose sign bit you want to test.

Return Value

Return ValueDescription
NonzeroThe sign bit is set (negative number or negative zero).
0The sign bit is clear.

C Examples

Example 1 — Basic Sign Bit Test

Check a negative and a positive value.

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

int main(void) {
    printf("signbit(-3.5) = %d\n", signbit(-3.5) ? 1 : 0);
    printf("signbit(3.5)  = %d\n", signbit(3.5) ? 1 : 0);
    return 0;
}

How It Works

  1. Include math.h. That provides the signbit macro.
  2. Negative input. Sign bit is set, so the test is true.
  3. Positive input. Sign bit is clear.
  4. Normalize print. Map nonzero to 1 for a clear display.

Example 2 — Catch Negative Zero

Show why a plain compare is not enough for signed zero.

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

int main(void) {
    double z = -0.0;

    printf("z == 0.0 ? %s\n", z == 0.0 ? "yes" : "no");
    printf("signbit(z) ? %s\n", signbit(z) ? "yes" : "no");
    return 0;
}

How It Works

  1. Make -0.0. A zero with the sign bit set.
  2. Compare to 0. Equality still says yes.
  3. Test signbit. The macro still reports the negative sign.
  4. When it matters. Logging, branch tables, and some numeric algorithms care.

Example 3 — Branch on the Sign Bit

Choose a message from the sign bit alone.

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

int main(void) {
    double values[3] = { 2.0, -0.0, -8.0 };
    int i;

    for (i = 0; i < 3; i++) {
        if (signbit(values[i])) {
            printf("value %d: sign bit set\n", i);
        } else {
            printf("value %d: sign bit clear\n", i);
        }
    }
    return 0;
}

How It Works

  1. Store samples. Include a negative zero in the list.
  2. Loop. Test each value with signbit.
  3. Branch. Print whether the bit is set.
  4. Notice zero. Index 1 is caught even though its magnitude is 0.

Frequently Asked Questions

signbit(x) reports whether the sign bit of floating value x is set. Nonzero means set; 0 means clear.
x is required. It is a float, double, or long double value to test.
A nonzero int if the sign bit is set, otherwise 0.
Include <math.h>.
Relational compares treat -0.0 as equal to +0.0. signbit can still see the negative sign bit on zero.

Next: sin()

Compute the gamma function Γ(x) for a floating argument.

sin() 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