CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?


Posted by: Pratyush Kumar on 11/17/2007

Contact Pratyush Kumar  Contact Pratyush Kumar
Category C Interview Questions
Rating (4.8) By 28 users
Added on 11/17/2007
Views 4022
Rate it!
Answers:

If you give value greater than .7 then it will go 2 the else part because .8 is not less than .7



 Posted by: neha    

Contact neha  Contact neha

this is because of rounding the value of variable a.
i.e.
a=0.7 is rounded to
=0.699999988
and the constant 0.7 is as
=0.69999999999
so a<0.7 ii true so it print "c"
but in case of 0.8
a=0.800000011 and
constant 0.8 is 0.8000000000000000



 Posted by: Mdhav Reddy. N    

Contact Mdhav Reddy. N  Contact Mdhav Reddy. N

because for floating point numbers 0.7 does not really means 0.7 but it is 0.699999999 so it happens



 Posted by: Manish Jain    

Contact Manish Jain  Contact Manish Jain

Here 0.7 is represented using considering it as double.Hence is the result.

To have the Desired result say..

if(a<0.7f) //Note : 'f'
{
...
}
else
{
..
}



 Posted by: vinay H.M    

Contact vinay H.M  Contact vinay H.M

if(a<0.7) condition is evaluated as false(0) by the compiler. so the output of program is 'c'.

whereas for 0.8 instead of 0.7, the condition is evaluated as true(1) so the output of the program is 'c++'.



 Posted by: Kinjal Patel    

Contact Kinjal Patel  Contact Kinjal Patel

the value 0.7 will be stored in variable a as 6.99999 which is less than 0.7



 Posted by: SHINOY M S    

Contact SHINOY M S  Contact SHINOY M S

In c any real number is treated as double.
So, when you compare 'a' with 0.7, complier is comparing float number with a double which results into printing - C.
To tell the compiler that you want to compare 'a' with float 0.7 you can change the program as follows:
#include<stdio.h>
void main()
{
float a = 0.7;
if(a <0.7f)
printf("C");
else
printf("C++");
}



 Posted by: rashmi bhatt    

Contact rashmi bhatt  Contact rashmi bhatt

Answer is C
since a is float.and 0.7 is double
when it compare a(float)<0.7(double)
means double is greater than float
so it is true .



 Posted by: Bipin    

Contact Bipin  Contact Bipin

A float number is always less than a double variable.Because 0.7 is by default a double variable.



 Posted by: Umakanta Mahanta    

Contact Umakanta Mahanta  Contact Umakanta Mahanta

This depends on the storage of a float value. The decimal values will be stored in binary form (....,2^3,2^2,2^1,2^0,.,2^-1,2^-2,2^-3,...)

So, when 0.7 is stored in multiples of 2 as .101b(which is not 0.7 but 0.699999988).Hence its value will be less than 0.7.

When the value of 0.8 is stored, it is stored as .111b(which is greater than 0.8 nearly 0.800000012).

Hence the same program with 0.8 gives C++ whereas 0.7 gives c as the output.

If someone thinks, if odd numbers will be print C, then it is wrong. Because for 0.5 there is an exact match in binary i.e .1b.So it will print C++.

NOTE: here 'b' refers to binary




 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed

answer will be c



 Posted by: debashis das    

Contact debashis das  Contact debashis das

All floating values like 0.1 or 0.7 r treated as double values internally.here a is float value and 0.7 is double value therefore double has more precisions than float.In comparison operation it checks each precision values. as all precision values are not equals it will go in else part and print c++.



 Posted by: shilpa    

Contact shilpa  Contact shilpa

floating point number can't be compared directly. So in order to compare the numbers we need to find the absolute value of the floating point (i.e precision) and then compare the number.



 Posted by: chini    

Contact chini  Contact chini

As the value of a is given as 0.7 the condition in if block is a<0.7 as it makes sense that 0.7=0.7 if block satisfies the condition.so it prints c.



 Posted by: naresh    

Contact naresh  Contact naresh

By default, the compiler takes value of 0.7 in the line " if (a < 0.7)" as double and that of a is float. Hence the difference. But for 0.8, the value of double and float are the same.



 Posted by: gavid    

Contact gavid  Contact gavid

This would give an error. As there should be a bracket after if and after else block ends.



 Posted by: Sanchay    

Contact Sanchay  Contact Sanchay

The float value always takes some value like 0.6999 which is less than 7. Hence c gets printed.
To get an exact 7, use double.



 Posted by: saurabh maur    

Contact saurabh maur  Contact saurabh maur

Output will be c++ because compiler takes 0.7 as the double.Thats why answer will be c++.



 Posted by: sudhanshu    

Contact sudhanshu  Contact sudhanshu

In C, floating point constant is stored as a long double,because of precision considerations.That's why,in 'a',instead of 0.7,0.666698 gets stored.Hence the answer....while comparing,do:
while(a<(float)0.7)



 Posted by: Sagar Natekar    

Contact Sagar Natekar  Contact Sagar Natekar

c++



 Posted by: karthik    

Contact karthik  Contact karthik

c



 Posted by: karthik    

Contact karthik  Contact karthik

This one i.e "a" will be treated as .699999 by the compiler not .7
Similarly .8 will be treated as .799999 by the compiler.



 Posted by: RANJAN SARKAR    

Contact RANJAN SARKAR  Contact RANJAN SARKAR

Whenever we use the floating point number then that floating point number is stored in to memory into the 32 bit (IEEE)(754) format that means in (sign, exponent, mantissa ).
if you will convert the 0.7 then the mantissa value will be .69999.so that it will print output "C".



 Posted by: Ravi     

Contact Ravi   Contact Ravi

we have problems with decimal to binary conversions for .7. it cannot b expressed correctly in finite binary. it is always rounded of to .69999999 for that .
howver .8 is exact in binary too



 Posted by: Sarathi    

Contact Sarathi  Contact Sarathi

There is a limitation of floating no in c.Variable cant store exact value of float.It stores some of less value.
In this case it store the less than .7 value.so if condition becomes true and answer will be c.



 Posted by: neha vora    

Contact neha vora  Contact neha vora

if u want that your code respond with the correct value then
try this

if(a<.7f)
{
cout<<"true";
}
else
{
cout<<"false";

}

this is bcoz in condition the value of the .7 is considered as double value you need to b it in float.



 Posted by: Anand dave    

Contact Anand dave  Contact Anand dave

float is not used in if statment



 Posted by: VINOD KALKUMBE    

Contact VINOD KALKUMBE  Contact VINOD KALKUMBE


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Code for swapping of two numbers without using temporary variable using C.
View Answer
code To draw a three dimensional graph using c graphics
View Answer
How to write a C program for displaying a sentence without output command?
View Answer
What is difference between the test effort and the test procedure?
View Answer
What are the disadvantages of using Pointers.
View Answer
How can I convert a number to a string?
View Answer
How to write a program for pascal triangle?
View Answer
write a program to remove comment lines and blank lines from an error free c program.
View Answer
What use of structure and union?
View Answer
Where does global, static, local, register variables, free memory and C Program instructions get stored?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12470/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.78