C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float a=0.7;
    if(a < 0.7f)
        printf("C\n");
    else
        printf("C++\n");
    return 0;
}
C
C++
Compiler error
Non of above
Answer: Option
Explanation:

if(a < 0.7f) here a is a float variable and 0.7f is a float constant. The float variable a is not less than 0.7f float constant. But both are equal. Hence the if condition is failed and it goes to else it prints 'C++'
Example:

#include<stdio.h>
int main()
{
    float a=0.7;
    printf("%.10f %.10f\n",0.7f, a);
    return 0;
}

Output:
0.6999999881 0.6999999881

Discussion:
3 comments Page 1 of 1.

Goutham said:   1 decade ago
Then why it is printing 0.6999.....81 instead of 0.700...0. Can anyone explain please?

Piyush said:   1 decade ago
%0.10f It specifies the number of digits to be printed after the decimal (.) whereas %f is the regular format specifier

Jordon said:   1 decade ago
What is the significance of .10f in printf ?

Post your comments here:

Your comments will be displayed after verification.