What is INFINITY In C?
INFINITY is a macro constant defined in the <math.h> library, and programmers use it to perform mathematical comparisons in C.
#include <math.h>
Example
C allows us to define INFINITY as positive INFINITY or negative INFINITY.
The snippet below shows how positive or unsigned INFINITY may be defined, and subsequently used in a mathematical comparison:
#include <math.h>#include <stdio.h>int main(){double pos_inf = INFINITY;double number = 112421421412124;if (number < pos_inf){printf("INFINITY wins!");}return 0;}
Negative INFINITY can be defined as demonstrated in the code below:
#include <math.h>#include <stdio.h>int main(){//Note that to define negative inifinity, we put '-' before INFINITYdouble pinf = -INFINITY;double neg_ninf = 112421421412124;if ( neg_ninf < pinf){printf("INFINITY wins!");}else if (neg_ninf > pinf){printf("INFINITY loses !");}return 0;}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved