-2
#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?

Subhiksh
  • 301
  • 2
  • 13

1 Answers1

1

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

juanchopanza
  • 223,364
  • 34
  • 402
  • 480