C Programming - Floating Point Issues - Discussion

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

if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than double constant 0.7. Hence the if condition is satisfied and it prints 'C'
Example:

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

Output:
0.7000000000 0.6999999881

Discussion:
21 comments Page 2 of 3.

Raghav Naganathan said:   1 decade ago
Guys...if the condition if(a < 0.7f) is used, then the output is c++, as 0.7f is a float constant and 0.7 is not less than 0.7 (as it is equal).

Hope this helps.

Suvadeep said:   1 decade ago
@Mahesh.

0.7f implies that 0.7 is a float constant and %.10f is the format specifier for printing a floating point number correct upto 10 decimal places.

Venkataramana said:   1 decade ago
Please anyone give me clear clarification. Above all are given different solutions but which one is believable? Please tell me.

Potnuru said:   1 decade ago
Why does the float constant has 0.69999991 and why not 0.700000000 only? some one explain.

Kedar said:   1 decade ago
Ya i agree with nive, initialise a=0.1 and check if a < 0.1

it prints C++ !!

Sankari said:   1 decade ago
Hey I'll search it. But I don't know how it prints C++. Please anyone explain?

Satyam kush said:   5 years ago
Hey guys.

Float a = 0.7d;
If(a<0.7)
Now it will give output C.

John said:   1 decade ago
Im not able to get it can someone give me a clear explanation.

Mahesh said:   1 decade ago
What does 0.7f and %.10f both implies? anyone please.

Krishna said:   1 decade ago
Why there float a=0.7 ----> (double constant, 0.7)


Post your comments here:

Your comments will be displayed after verification.