Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is signbit in C?

Sheza Munir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The signbit function in C determines whether a floating-point number is negative.

The signbit of a negative number is set as 1

This function checks if the given number’s signbit is set.

Library

The signbit function can be accessed via the math.h library.

#include <math.h>

Parameters

The signbit function only takes one parameter: the floating-point number that needs to be ascertained as negative.

Syntax

The syntax for the function call is:

int returned_val = signbit(x);

Return value

The function will return 0 if the floating-point parameter is non-negative (i.e., the signbit is not set). Otherwise, the number is negative and a non-zero number is returned.

Code

#include <stdio.h>
#include <math.h>
int main() {
double num1 = 1.2;
double num2 = -1.2;
// signbit returns 0
printf("num1 is %s\n", signbit(num1)? "negative" : "not negative");
// signbit returns non-zero number
printf("num2 is %s\n", signbit(num2)? "negative" : "not negative");
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring