code_junkie

Why does this C code produce a double instead of a float?

Nov 10th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. celsius = (5.0/9.0) * (fahr-32.0);
  2.  
  3. celsius = (5.0F/9.0F) * (fahr-32.0F);
  4.  
  5. #include <stdio.h>
  6. int main() {
  7. float celsius;
  8. float fahr = 212;
  9. printf("sizeof(celsius) ---------------------> %dn", sizeof(celsius));
  10. printf("sizeof(fahr) ------------------------> %dn", sizeof(fahr));
  11. printf("sizeof(double) ----------------------> %dn", sizeof(double));
  12. celsius = (5.0f/9.0f) * (fahr-32.0f);
  13. printf("sizeof((5.0f/9.0f) * (fahr-32.0f)) --> %dn", sizeof((5.0f/9.0f) * (fahr-32.0f)));
  14. printf("sizeof((5.0/9.0) * (fahr-32.0)) -----> %dn", sizeof((5.0/9.0) * (fahr-32.0)));
  15. printf("celsius -----------------------------> %fn", celsius);
  16. }
  17.  
  18. sizeof(celsius) ---------------------> 4
  19. sizeof(fahr) ------------------------> 4
  20. sizeof(double) ----------------------> 8
  21. sizeof((5.0f/9.0f) * (fahr-32.0f)) --> 4
  22. sizeof((5.0/9.0) * (fahr-32.0)) -----> 8
  23. celsius -----------------------------> 100.000008
Add Comment
Please, Sign In to add comment