C Programming - Library Functions - Discussion

2. 

What will be the output of the program?

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

int main()
{
    float i = 2.5;
    printf("%f, %d", floor(i), ceil(i));
    return 0;
}

[A]. 2, 3
[B]. 2.000000, 3
[C]. 2.000000, 0
[D]. 2, 0

Answer: Option C

Explanation:

Both ceil() and floor() return the integer found as a double.

floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000.

ceil(2.5) returns 3, while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.


Anusha said: (Mar 15, 2011)  
Can anyone please explain how it becomes 0 ?

Archana said: (Mar 17, 2011)  
Please give me clarity how it became 0 ans may be 2, 3 also but you gave those values why?

Sundar said: (Mar 17, 2011)  
@Anusha

Use the casting operator (int) along with ceil(). This will convert the 'double' value to 'int' value.

printf("%f, %d", floor(i), (int)ceil(i));

The output will be : 2.000000, 3

Prakash said: (Jul 12, 2011)  
ceil(2.5)it returns 3,
while converting the double to int it returns '0'.
So, the output is '2.000000, 0'.

Swathi said: (Aug 30, 2011)  
Can anyone please explain me the procedure of double to int conversion ?

Chandana said: (May 4, 2012)  
Conversion is confusing please explain the conversion of double to integer.

Abc said: (May 6, 2012)  
Can anyone explain double to int conversion.

Priyanka said: (Sep 3, 2012)  
Please explain the conversion of double to integer.

Raghav said: (Oct 16, 2012)  
Double is 8 bytes and Int is 4 bytes. Hence specifying %d will yield in first 4 bytes from right which is Zero.

Shubham said: (Mar 26, 2013)  
This is really confusing. Please somebody explain clearly.

Djv said: (Aug 9, 2013)  
ceil(2.5) returns '3'.

As double is 8 bytes- 3 is represented as :

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000011

So while converting double to int(which is of 4 bytes).

The first four bytes will be taken into consideration.

Hence returning the value '0' :).

Ramya said: (Aug 10, 2013)  
Ceil always return double value. But here return type is integer while converting double to integer 8 bytes is converted to 4 byte real value. So four bits from right to return zero.

Ganesh Singh said: (Jan 18, 2014)  
I have got the o/p=0; but now I am confused with the concept used in the following case given below:

int main()
{
double a=3;
int b=a;
printf("%d",b);
}

Please make me understood soon as possible.

Prince Rambade said: (Jun 8, 2014)  
Ok if i assume above explanation to be correct. Then pls explain how below program result in,

Output : 0,0.00000000 ?

int main()
{
float i = 2.5;
printf("%d, %f", floor(i), ceil(i));
return 0;
}

ceil() returns a double & I have printed the value with %f format specifier but it results in 0.000000 how ?

Cvam Singh said: (Sep 18, 2014)  
Please specify that it will return from right or left.

Amarjeet said: (Jun 12, 2016)  
It prints 0 because doubles priority is higher than an integer.

That's why it prints 2.000000 and 0.

Swapnil Kudale said: (Nov 10, 2017)  
@Djv is correct.

Rakhi Sinha said: (May 18, 2020)  
GCC compiler is giving some garbage value instead of 0.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.