Go to my personal blog
Another program to print Fahrenheit-celsius table with decimal integer
This program is presented as below.
#include <stdio.h>/* print Fahrenheit_celsius table for Fahr = 0, ..., floating-point version */int mai N () { float Fahr, celsius; int lower, upper, step; lower = 0; /* Lower limit of temperature table */ upper =; /* Upper limit of temperature table */ step =; /* Step size */ Fahr = lower; while (Fahr <= Upper) { Celsius = (5.0/9.0) * (fahr-32.0); printf ("%3.0f%6.1f\n", Fahr, Celsius); Fahr = Fahr + step; } return 0;}
The figure of this program is presented as above. The right part of the and is the output. This was much like the program which was mentioned at the beginning of the article, except thatfahr
andcelsius
is declared to be float. We were unable to use 5/9 in the previous version because integer division would truncate it to zero. A decimal point in a constant indicates that it's floating point, however, so 5.0/9.0 are not truncated because it is the ratio of the floating-point values.
If an arithmetic operator have integer operands, an integer operation is performed. If An arithmetic operator have one floating-point operand and one integer operand, however, the integer would be con Verted to floating point before the operation are done.Writing floating-point constants with explicit decimal points even when they has integral values emphasizes their floatin G-point Nature for Human readers.
For now, notice the assignment
Fahr = lower;
And the test
while (Fahr <= Upper)
Also work in the Nature way-the int are converted to float before the operation are done.
The implications of width and precision are tabled as follows.
- %d Print as decimal integer
- %6d Print as decimal integer, at least 6 characters wide
- %f Print as Foating point
- %6f print as floating point, at least 6 characters wide
- %.2f print as floating point, 2 characters after decimal point
- %6.2f print as floating point in leat 6 wide and 2 characters after decimal point
Among others,printf
also recognizes%o
For octal,%x
For hexadecimal,%c
For character,%s
For Charater string, and%%
For%
itself.
Referencethe C progrmming Language
A program-to-print Fahrenheit-celsius table with floating-point values