# 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;
}
```

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 1 of 3.
Satyam kush said:
3 years ago

Hey guys.

Float a = 0.7d;

If(a<0.7)

Now it will give output C.

Float a = 0.7d;

If(a<0.7)

Now it will give output C.

Aneesh said:
6 years ago

Hey Guys!

After looking for solutions here i found some gave declaration as odd value in float prints "c" and even value prints "c++".

I want to clarify that this is not true,

Float value from 0.2-0.6 gives odd & even both gives output as "c++" but 0.7 gives output as "c" and again 0.8 is printed as "c++" but its different with 0.9 it prints "c".

Looking for a perfect solution please to share it will help a lot.

Thank you.

After looking for solutions here i found some gave declaration as odd value in float prints "c" and even value prints "c++".

I want to clarify that this is not true,

Float value from 0.2-0.6 gives odd & even both gives output as "c++" but 0.7 gives output as "c" and again 0.8 is printed as "c++" but its different with 0.9 it prints "c".

Looking for a perfect solution please to share it will help a lot.

Thank you.

Ichidan said:
9 years ago

The answer is, it depends on the compiler and target architecture.

You see float & double precision are encoded as 32 & 64 bit values. The encoding structure can be found on IEEE standards (Google float IEEE). The value 0.7 cannot be represented as either a float or a double. It can only be approximated. This approximation is more accurate with double precision than single (float) precision.

Now some compiler-architectures are 32-bit. Especially the systems of the 90s. So what happens is:

float x = 0.7;

If (x == 0.7) printf ("32-bit x is the same value as 32-bit 0.7. This compiler defaults to 32-bit precision, perhaps this is 32-bit only hardware\n");

Else printf ("32-bit x is not the same value as 64-bit 0.7. This compiler defaults to 64-bit precision perhaps this is 64-bit hardware\n");

You see float & double precision are encoded as 32 & 64 bit values. The encoding structure can be found on IEEE standards (Google float IEEE). The value 0.7 cannot be represented as either a float or a double. It can only be approximated. This approximation is more accurate with double precision than single (float) precision.

Now some compiler-architectures are 32-bit. Especially the systems of the 90s. So what happens is:

float x = 0.7;

If (x == 0.7) printf ("32-bit x is the same value as 32-bit 0.7. This compiler defaults to 32-bit precision, perhaps this is 32-bit only hardware\n");

Else printf ("32-bit x is not the same value as 64-bit 0.7. This compiler defaults to 64-bit precision perhaps this is 64-bit hardware\n");

Venkataramana said:
9 years ago

Please anyone give me clear clarification. Above all are given different solutions but which one is believable? Please tell me.

Shafi said:
9 years ago

@Ankitha.S.

You try with 0.5 it gives you c++ why?

All the odd values after decimal will give the same as you assume, but 0.5 is not giving answer C it is giving C++.

You try with 0.5 it gives you c++ why?

All the odd values after decimal will give the same as you assume, but 0.5 is not giving answer C it is giving C++.

Ankita s said:
1 decade ago

Guys. I found that when I was compiling with 0.7 value. I was getting "c" as output same with 0.9 or 0.1. As I think while entering odd values after decimal. Its output is "c"

And while entering even values its output is "C++"

Odd.

0.9

It is checking,

if a<0.9

i.e 0.899999762<0.9

Even...

0.8

It is checking

if a<0.8

i.e 0.80000000<0.800000119

And while entering even values its output is "C++"

Odd.

0.9

It is checking,

if a<0.9

i.e 0.899999762<0.9

Even...

0.8

It is checking

if a<0.8

i.e 0.80000000<0.800000119

Sankari said:
1 decade ago

Hey I'll search it. But I don't know how it prints C++. Please anyone explain?

Prasad said:
1 decade ago

Guys if the value in 0.70000 (the range after decimal don't include 7 from next is between (0 to 5) is considered as double and (6 to 9) is considered as float) so here in 0.6999 we have 9 so it is considered as float and 0.7000 is considered as double because of 0 in it.

Ramya said:
1 decade ago

Hey I am also searching for that. But couldn't get ya. From 0.1 to 0.6 it prints c++ when it is 0.7 it prints c. How it is possible. If anything special for 0.7 as floating point ? if anyone knows please help me!

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.

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.

Post your comments here:

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers