Ceil, Floor, Absolute Value and Remainder Functions in C

Question: What are Floor, Ceiling, Absolute Value and Remainder Functions in Standard C Library?

Answer: Standard C Library defines ‘floor()’, ‘ceil()’, ‘fabs()’ and ‘fmod()’ functions with return values and their arguments as type ‘double’ because of greater range of double values over ‘integer’ values. These are prototyped as below,

    double floor(double value);
    double ceil(double value);
    double fabs(double value);
    double fmod(double val1, double val2);

Notice that ‘floor()’ function returns largest integral value not greater than its argument. ‘ceil()’ function returns smallest integral value not less than its argument. ‘fabs()’ returns absolute value of its argument and fmod() returns remainder of division of val1 by val2. Let’s take examples of ‘floor()’ and ‘ceil()’ functions below

/* fp_floor_fun.c -- 'floor()' returns largest integer not greater */
/* than its argument */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main(void)
{
    double value, result;
 
    printf("User, enter value for 'floor' evaluation...\n");
    scanf("%lf", &value);
 
    /* let's evaluate 'floor' of 'value' */
    result = floor(value);
 
    printf("'floor' of \"%lf\": %lf\n", value, result);
 
    return 0;
}
User, enter value for 'floor' evaluation...
3.5
'floor' of "3.500000": 3.000000
 
User, enter value for 'floor' evaluation...
-122
'floor' of "-122.000000": -122.000000
 
User, enter value for 'floor' evaluation...
-12.23
'floor' of "-12.230000": -13.000000
/* fp_ceil_fun.c -- 'ceil()' returns smallest integer not less */
/* than its argument */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main(void)
{
    double value, result;
 
    printf("User, enter value for 'ceiling' evaluation...\n");
    scanf("%lf", &value);
 
    /* let's evaluate 'ceiling' of 'value' */
    result = ceil(value);
 
    printf("'ceil' of \"%lf\": %lf\n", value, result);
 
    return 0;
}
User, enter value for 'ceiling' evaluation...
23.3
'ceil' of "23.300000": 24.000000
 
User, enter value for 'ceiling' evaluation...
-12.453
'ceil' of "-12.453000": -12.000000

Try to work-out other two interesting functions to understand them by implementing.

advertisement
advertisement
Note: Join free Sanfoundry classes at Telegram or Youtube

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.