C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Yes / No Questions (Q.No. 1)
1.
Will the printf() statement print the same values for any values of a?
#include<stdio.h>
int main()
{
    float a;
    scanf("%f", &a);
    printf("%f\n", a+a+a);
    printf("%f\n", 3*a);
    return 0;
}
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 2 of 2.

Ranjana said:   1 decade ago
It will not print any value for the first printf statement and for 2nd and third it assumes the value0.0000 because no declaration or input is value is given of a so it print 0.0000 both time.

Seifdean said:   1 decade ago
In that case a is not initialized so it will take an arbitrary value not 0, other thing &a is the address of a how can have the same value of 3*a is this coincidence ?

Kalai said:   1 decade ago
According to the ans...

Consider a=0.1.. scanf func reads value of a
then a+a+a=0.1+0.1+0.1=0.3
next 3*0.1=0.3

So it prints the same value.


Post your comments here:

Your comments will be displayed after verification.