edited by
1,594 views
3 votes
3 votes
main() {
float a=.5, b=.7;
if(b<.7)
  if(a<.5)
   printf("TELO");
  else
   printf("LTTE");
else
 printf("JKLF");

}
edited by

1 Answer

Best answer
6 votes
6 votes

C provides three floating types , corresponding to different floating-point formats:

$$\text{float} \rightarrow \text{Single-precision floating point}\\ \text{double} \rightarrow \text{Double-precision floating point}\\ \text{long double} \rightarrow \text{Extended-precision floating point}$$

$float $ is suitable when the amount of precision isn't critical.

$double$ provides greater precisions.

$\text{long double}$ provides ultimate precision, it is rarely used.

Now, C standard doesnot provide how much precision $\text{float, double, long double}$ provides. So, Most computers follow IEEE Standard $754$.

Single precision (float) gives 23 bits of significant, 8 bits of exponent, and 1 sign bit.

Double precision (double) gives 52 bits of significant, 11 bits of exponent, and 1 sign bit.

 By default $float $ -ing constants are stored as double-precision numbers. When a C compiler finds $13.0$(assume) in a program, it arranges for the number to be stored in memory in the same format as a $double$ variable. This rule generally cause no problems, since $double$ values are converted automatically to $float$ when necessary.

main() {
float a=.5, b=.7;
if(b<.7)
  if(a<.5)
   printf("TELO");
  else
   printf("LTTE");
else
 printf("JKLF");

}

We're using $\color{blue}{float}$ to store $.5$ & $.7$. But $.5$ & $.7$ will be trated as  $\color{red}{double}$.

Now, .5 can be represented in binary form as $001111110000$ , which means $.5$, no error during conversion

but .7 can be represented in binary form as $00111111001100110011001100110011$, which actually means $0.699999988079071044921875$. So, there is an error during conversion which is $\approx -1.1920928955078125E-8$.

Now, as $.5$ & $.7$ both are considered as $double$, & be followed by $\color{blue}{\text{Double-precision format(64-bit)}}$ , then 

$$.5 \rightarrow 00111111000000000000000000000000\\ .7 \rightarrow 00111111001100110011001100110011$$

but as we stored $.5$ & $.7$ into  float $a$ & $b$ respectively. & we know, $float$ uses $\text{single precision format(32-bit)}$, so 

$$.5 \text{will be treated as 0.5.... (no change during conversion)}\\.7 \text{will be treated as 0.6999.... (changes during conversion)}$$

& $a's$ value is now $0.5$

$b's$ value is now $0.699...$

So, 

if(b<.7)

is true.

Now, 

if(a<.5)

this does not satisfy the condition.

So, we go to the $\color{red}{else}$ part & prints $\color{violet}{LTTE}$

selected by

Related questions

0 votes
0 votes
0 answers
1
iarnav asked Dec 26, 2017
421 views
what is the o/p?int main() { char inchar = 'A'; switch (inchar) { case 'A' : printf ("choice A \n") ; case 'B' : { printf ("choice B") ; break; } case 'C' : case 'D' : ca...
1 votes
1 votes
2 answers
2
rahul sharma 5 asked Dec 14, 2016
475 views
For n=2 the P is coming as 3,but none of the option is satisfying?
0 votes
0 votes
1 answer
3