C Programming - Library Functions - Discussion

5. 

What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    char *i = "55.555";
    int result1 = 10;
    float result2 = 11.111;
    result1 = result1+atoi(i);
    result2 = result2+atof(i);
    printf("%d, %f", result1, result2);
    return 0;
}

[A]. 55, 55.555
[B]. 66, 66.666600
[C]. 65, 66.666000
[D]. 55, 55

Answer: Option C

Explanation:

Function atoi() converts the string to integer.
Function atof() converts the string to float.

result1 = result1+atoi(i);
Here result1 = 10 + atoi(55.555);
result1 = 10 + 55;
result1 = 65;

result2 = result2+atof(i);
Here result2 = 11.111 + atof(55.555);
result2 = 11.111 + 55.555000;
result2 = 66.666000;
So the output is "65, 66.666000" .


Sri said: (Dec 6, 2011)  
What is the difference between float and double?

Ashok Phour said: (May 11, 2012)  
Float have 4 bytes but double have 8 bytes.
So any number 2.3 both represents its binary value in the 4 bytes and 8 bytes in different ways.

Float have 4 bytes 32-bit so in 4 bytes first byte have seven bit for store EXPONENT DATA bit 8th bit MANTISSA sign bit and remaining three bytes have 23 bit for MANTISSA DATA bit and 1 bit for EXPONENT sign bit.
Double have 8 byte so place the binary value in different way.so for the same value both have different bit placing and different values.

Thomsika said: (May 14, 2013)  
What is the difference between char and float?

Prabakaran said: (Sep 4, 2013)  
I have one Program. But I didn't know the answer. Can anyone please explain the program and the solving method. The program is,

main()
{
int a=10;
printf("%d &i",a,10);
}

Deepika Gupta said: (Dec 2, 2013)  
@Prabakaran.

It's output is 10&i. Because in printf statement, only one access specifier is used for 2 integer value.

Niky said: (May 5, 2015)  
Why not taken 55.55 to 56 as in cell problem there was 2.5 as 3 not 2?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.