#include<stdio.h>
#include<math.h>
int main()
{
float i = 2.5;
printf("%d, %f", floor(i), ceil(i));
return 0;
}
Although I have used %f specifier in printf() for ceil, it prints 0.000000. Why is this so?
#include<stdio.h>
#include<math.h>
int main()
{
float i = 2.5;
printf("%d, %f", floor(i), ceil(i));
return 0;
}
Although I have used %f specifier in printf() for ceil, it prints 0.000000. Why is this so?
The return type of floor
is a floating point number (double
in this case). Passing it with "%d"
yields undefined behaviour.
You need to use the %f
format specifier. For example,
#include <stdio.h>
#include<math.h>
int main()
{
float i = 2.5;
printf("%f, %f", floor(i), ceil(i));
return 0;
}
Output
2.000000, 3.000000