One big difference is that of negative numbers; if you change myF to -5.6, then casting to an int returns -5 while floor(myF) is -6.

As to which is preferable, as a rule of thumb I'd say to only cast to an int if you know that's what you need -- and since you're asking here, chances are that you probably want floor.

(Also note that with printf formatting, %ld is a long integer; a double is %lf.)

Answer from Mark Rushakoff on Stack Overflow
🌐
W3Schools
w3schools.com › c › ref_math_floor.php
C Math floor() Function
The floor() function is defined in the <math.h> header file. Tip: To round a number UP to the nearest integer, look at the ceil() function.
🌐
Scaler
scaler.com › home › topics › floor() in c
floor() Function in C - Scaler Topics
June 16, 2024 - The floor() is a library function in C defined in the <math.h> header file. This function returns the nearest integer value, which is less than or equal to the floating point number (float or double) passed to it as an argument.
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
September 27, 2024 - Include the math.h header file in your C program since floor() is part of this library. Declare a floating-point variable, and assign it a value. Use the floor() function to round down the variable to the nearest integer.
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library: floor function
C Standard Library: floor Function
August 29, 2012 - The C library floor() function of type double accept the single parameter(x) to return the largest integer value less than or equal to, by the given values.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › floor.php
C Language: floor function (Floor)
In the C Programming Language, the floor function returns the largest integer that is smaller than or equal to x (ie: rounds downs the nearest integer).
🌐
Programiz
programiz.com › c-programming › library-function › math.h › floor
C floor() - C Standard Library
The floor() function calculates the nearest integer less than or equal to the argument passed.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 116833-floor-int.html
floor() and int()
I always thought that if the values are integer and positive the would both return the same value...but that is the only time that is the case... ... (int) floor (1.1) = 1 (int)1.1 = 1 If they were negative and integer then ceiling and truncation would give the same
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
#include <math.h> #include <stdio.h> int main(void) { printf("floor(+2.7) = %+.1f\n", floor(2.7)); printf("floor(-2.7) = %+.1f\n", floor(-2.7)); printf("floor(-0.0) = %+.1f\n", floor(-0.0)); printf("floor(-Inf) = %+f\n", floor(-INFINITY)); } ... Retrieved from "https://en.cppreference.com/...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-floor-function
C floor() Function - GeeksforGeeks
July 7, 2024 - The floor(x) function in C is used to compute the largest integer value less than or equal to a given number.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re79.html
floor - C in a Nutshell [Book]
December 16, 2005 - The floor() function returns the greatest integer that is less than or equal to its argument. However, the function does not have an integer type; it returns an integer value, but with a floating-point type.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Cplusplus
cplusplus.com › reference › cmath › floor
Floor
double floor (double x); float floor (float x);long double floor (long double x); double floor (T x); // additional overloads for integral types ... Rounds x downward, returning the largest integral value that is not greater than x. Header <tgmath.h> provides a type-generic macro version of this function. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › floor
std::floor, std::floorf, std::floorl - cppreference.com
#include <cmath> #include <iostream> int main() { std::cout << std::fixed << "floor(+2.7) = " << std::floor(+2.7) << '\n' << "floor(-2.7) = " << std::floor(-2.7) << '\n' << "floor(-0.0) = " << std::floor(-0.0) << '\n' << "floor(-Inf) = " << std::floor(-INFINITY) << '\n'; }
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
C floor() function (math.h): The floor() function is used to calculate the largest integer that is less than or equal to x.
🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › floor
C++ floor() - C++ Standard Library
The floor() function in C++ returns the largest possible integer value which is less than or equal to the given argument.
🌐
Tutorial Gateway
tutorialgateway.org › c-floor-function
C floor Function
April 5, 2025 - The C floor function is a Math function that returns the closest integer value, which is less than or equal to a given number.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 007904975 › functions › floor.html
floor
The integral value returned by these functions might not be expressible as an int or long. The return value should be tested before assigning it to an integer type to avoid the undefined results of an integer overflow. The floor() function can only overflow when the floating-point representation has DBL_MANT_DIG > DBL_MAX_EXP.