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 2 of 3.
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.
Potnuru said:
1 decade ago
Why does the float constant has 0.69999991 and why not 0.700000000 only? some one explain.
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.
Hope this helps.
Shiwam said:
1 decade ago
Hey gys!
it prints c++ for a=0.1 to 0.6
and from 0.7 and afterwards it prints c.
again from 0.8 it prints c++.
it implies 0.7 is a special case that may be related to range of float and double.
so i m searching and pls you also.
Good luck.
it prints c++ for a=0.1 to 0.6
and from 0.7 and afterwards it prints c.
again from 0.8 it prints c++.
it implies 0.7 is a special case that may be related to range of float and double.
so i m searching and pls you also.
Good luck.
Shak said:
1 decade ago
#include<stdio.h>
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}
Why it prints the c++ as output? please explain.
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}
Why it prints the c++ as output? please explain.
Krishna said:
1 decade ago
Why there float a=0.7 ----> (double constant, 0.7)
Manoj said:
1 decade ago
Fantastic.
Kedar said:
1 decade ago
Ya i agree with nive, initialise a=0.1 and check if a < 0.1
it prints C++ !!
it prints C++ !!
Nive said:
1 decade ago
#include<stdio.h>
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}
why it prints the c++ as output? pls xplain..........
int main()
{
float a=0.8;
if(a < 0.8)
printf("C\n");
else
printf("C++\n");
return 0;
}
why it prints the c++ as output? pls xplain..........
John said:
1 decade ago
Im not able to get it can someone give me a clear explanation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers