GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 406 of 453    Print  
Datatypes Justify Output
#include
void main()
{
float a=0.7;
if(a<0.7)
printf("yes");
else
printf("no");
}
what is the answer and reason.



  
Total Answers and Comments: 5 Last Update: July 31, 2009     Asked by: patchillaramkumar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: ananth3335
 
in c, the floating point number shouldn't used in codition statement.
the compiler take like below for given code,
if(0.699<0.7)
so , the output will be "yes"

Above answer was rated as good by the following members:
RoMBiN, munnaspartacus, lifeisbeautiful
March 31, 2008 01:46:41   #1  
ananth3335 Member Since: February 2008   Contribution: 14    

RE: Datatypes Justify Output
in c the floating point number shouldn't used in codition statement.
the compiler take like below for given code
if(0.699<0.7)
so the output will be "yes"

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
April 11, 2008 06:11:52   #2  
pavan.mustyala Member Since: April 2008   Contribution: 7    

RE: Datatypes Justify Output
All floating point values are stored as double. So in the given program the value of variable "a" is interpreted as 0.69999999 thus "double" is truncated to "float".

Even Some compillers will generate a warning as follows :

initializing' : truncation from 'double' to 'float'

This is one possible reason for the wrong output.

NOTE : Also during condition checking there will be implicit type conversion among the different data types and their values thus leading to unexpected results.So one should be very careful when checking for variables of different data types.

The if condition in the above can be modified as

if(a < (float)0.7) to get the correct output(expected output).


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
April 11, 2008 13:03:40   #3  
ashoksurati Member Since: April 2008   Contribution: 13    

RE: Datatypes Justify Output
I go with my friend Pawan.

The output of the above program is Yes.

ThankU
Ashok

 
Is this answer useful? Yes | No
June 06, 2008 03:50:48   #4  
shweta mathur Member Since: June 2008   Contribution: 1    

RE: Datatypes Justify Output
When
a 0.7
internally compiler treats 0.7 as double.
Now a is a float and float is always small then double so the if condition is true and answer will be

YES

 
Is this answer useful? Yes | No
July 31, 2009 08:42:37   #5  
gauravmitbhu Member Since: July 2009   Contribution: 1    

RE: Datatypes Justify Output


#include<stdio.h>
#include<conio.h>


void main(voif)
{
float a 0.9 b 0.9;
double c 0.9;
if(a<b)
printf("yes for b");
else
printf("no for b");

if(a<c)
printf("nyes for c");
else
printf("nno for c");

getch();
}

Output:
no for b
yes for c


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape