我的主力博客:半亩方塘

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, 20, ..., 300; floating-point version */
int main()
{
float fahr, celsius;
int lower, upper, step; lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit of temperature table */
step = 20; /* 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;
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJuZXJ3YW5nMjAxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

The figure of this program is presented as above. The right part of the figure is the output. This is much like the program which is mentioned at the beginning of the article, except that fahr and celsius are
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 is floating point, however, so 5.0/9.0 is not truncated because it is the ratio of two floating-point values
.

If an arithmetic operator has integer operands, an integer operation is performed. If
an arithmetic operator has one floating-point operand and one integer operand, however, the integer will be converted to floating point before the operation is done
. Writing floating-point constants with explicit decimal points even when
they have integral values emphasizes their floating-point nature for human readers.

For now, notice that the assignment

fahr = lower;  

and the test

while (fahr <= upper)

also work in the nature way — the int is converted to float before the operation is 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, at 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.

Reference

最新文章

  1. Chrome出了个小bug:论如何在Chrome下劫持原生只读对象
  2. 企业SOA架构设计理论
  3. php面试题2
  4. php栈数据结构和括号匹配算法
  5. Jpinyin笔记
  6. PostgreSQL数据库基本配置
  7. ERStudio的使用
  8. cvLoadImage函数解析 cvLoadImageM()函数
  9. 百度ue富文本编辑器setContent方法报错初始化加载内容失败解决办法
  10. MongoDB基本操作命令
  11. PHP如何提取img标签属性
  12. radio组件
  13. 二次战CPP链表
  14. java科学和艺术语言 第六章 课后编程
  15. 第三方控件netadvantage UltraWebGrid如何生成带加号多级表数据也就是带子表
  16. mysql中的一些操作语句,留存
  17. 疯狂的采药 洛谷p1616
  18. python爬虫循环导入MySql数据库
  19. 关于Linux虚拟化技术KVM的科普 科普二(KVM虚拟机代码揭秘)
  20. git pull更新错误解决办法

热门文章

  1. 从C#程序中调用非受管DLLs
  2. Windows下如何使用CMD命令进入MySQL数据库
  3. 专题训练——[kuangbin带你飞]最短路练习
  4. Python学习-列表的转换和增加操作
  5. Python之面向对象类和对象
  6. Quartz--01
  7. 集训第六周 古典概型 期望 C题
  8. 配置bean
  9. noip模拟赛 轮换
  10. 【BZOJ4559】成绩比较(组合计数,容斥原理)