C floor() Function

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

Definition

In C, floor() rounds x down toward negative infinity. floor(2.9) is 2, and floor(-2.1) is -3.

You pass one double. The result is still a double, not an int. Include <math.h>.

Syntax

Pass the number you want to round down. Start with this call.

C
floor(2.9);
CallWhat it does
floor(2.9)Rounds down to 2.

Include <math.h>. On GCC, link with -lm. This is not the same as cutting off the fraction. For that, use trunc().

Library declarations
C
double floor(double x);
float floorf(float x);
long double floorl(long double x);

Parameters

ParameterDescription
xRequired. The double to round toward -infinity.

Return Value

Return ValueDescription
doubleThe largest integer value that is not greater than x, stored as a double.

C Examples

Example 1 — Basic Floor

Round a positive non-integer down to the previous whole number.

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

int main(void) {
    printf("%.1f\n", floor(5.78));
    printf("%.1f\n", floor(5.0));
    return 0;
}

How It Works

  1. Include math.h. That declares floor.
  2. 5.78 rounds down. The largest integer ≤ 5.78 is 5.
  3. 5.0 stays 5.0. Whole numbers are left unchanged.
  4. Print as double. %.1f shows one decimal place.

Example 2 — Negative Numbers

Toward -infinity means negatives with a fraction become more negative—not closer to zero.

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

int main(void) {
    printf("floor(-2.3) = %.1f\n", floor(-2.3));
    printf("ceil(-2.3)  = %.1f\n", ceil(-2.3));
    return 0;
}

How It Works

  1. floor goes down the number line. From -2.3, the next integer toward -infinity is -3.
  2. ceil goes the other way. ceil(-2.3) is -2.
  3. Not “toward zero.” That job belongs to trunc.
  4. Compare both. Side-by-side output makes the directions clear.

Example 3 — How Many Full Batches Fit

Count complete groups when a partial group does not count.

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

int main(void) {
    int items = 25;
    int batch = 10;
    int full = (int)floor(items / (double)batch);

    printf("full batches = %d\n", full);
    return 0;
}

How It Works

  1. Divide as double. 25 / (double)10 is 2.5.
  2. Floor the quotient. floor(2.5) is 2.0—only complete batches.
  3. Cast for printing. Safe here because the count is small.
  4. Contrast with ceil. Pages needed would use ceil; leftover ignored uses floor.

Frequently Asked Questions

floor() rounds x down toward negative infinity. floor(2.9) is 2, and floor(-2.1) is -3.
x is required. It is the double to round.
A double holding an integer value. It is not an int.
Include <math.h>. On GCC, link with -lm.
No. floor() goes toward negative infinity. trunc() goes toward zero, so trunc(-2.1) is -2.

Next: fma()

Get the floating-point remainder after dividing two doubles.

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