我的主力博客:半亩方塘

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

A program to print Fahrenheit-Celsius table with floating-point values的更多相关文章

  1. @media print样式 关于table断页

    <html> <head> <style> @media print { table { page-break-after:auto } tr { page-bre ...

  2. python3变量和数据类型

        变量和数据类型 知识点 python 关键字 变量的定义与赋值 input() 函数 字符串的格式化 实验步骤 每一种编程语言都有它们自己的语法规则,就像我们所说的外语. 1. 关键字和标识符 ...

  3. Python3简明教程(二)—— 变量和数据类型

    关键字和标识符 下列的标识符是Python3的关键字,并且不能用于通常的标识符.关键字必须严格按照下面的拼写: False def if raise None del import return Tr ...

  4. ABAP program lines are wider than the internal table

    错误详细描述: An exception occurred that is explained in detail below.The exception, which is assigned to ...

  5. Why is celsius = 5 * (fahr - 32) / 9 ?

    Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...

  6. Codeforces Round #344 (Div. 2) B. Print Check

    B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Python multi-thread 多线程 print 如何避免print的结果混乱

    multithread如何写 这是我第一次写multithread,所以就是照着例子学,下面是我用来学的例子 来自于”Automate the boring stuff with Python”的15 ...

  8. Calling Lua From a C Program

    Introduction From a running C program, you can call a Lua script. The C program can pass arguments t ...

  9. Codeforces Round #344 (Div. 2) B. Print Check 水题

    B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...

随机推荐

  1. 四种方案解决ScrollView嵌套ListView问题 [复制链接]

    以下文章转自@安卓泡面 在工作中,曾多次碰到ScrollView嵌套ListView的问题,网上的解决方法有很多种,但是杂而不全.我试过很多种方法,它们各有利弊. 在这里我将会从使用ScrollVie ...

  2. 使用webpack搭建react项目 webpack-react-project

    webpack-react-project 使用webpack搭建react项目 webpack搭建react项目 github源码 具体配置信息参照package.json和webpack.conf ...

  3. PC端样式重置

    html{font-family:"Microsoft YaHei UI","Microsoft YaHei",sans-serif;-ms-text-size ...

  4. [Python3网络爬虫开发实战] 1.8.1-pyspider的安装

    pyspider是国人binux编写的强大的网络爬虫框架,它带有强大的WebUI.脚本编辑器.任务监控器.项目管理器以及结果处理器,同时支持多种数据库后端.多种消息队列,另外还支持JavaScript ...

  5. [Python3网络爬虫开发实战] 1.7.1-Charles的安装

    Charles是一个网络抓包工具,相比Fiddler,其功能更为强大,而且跨平台支持得更好,所以这里选用它来作为主要的移动端抓包工具. 1. 相关链接 官方网站:https://www.charles ...

  6. cookie和session的区别及session的生命周期

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录 ...

  7. Matlab学习笔记(二)

    二.MATLAB基础知识 (二)数值.变量和表达式 命名规则: 变量名对大小写敏感,即区分大小写 变量名必须以字母开头,后面可以采用数字.下划线和字母,但不能使用空格.标点符号和运算符 变量名最长可以 ...

  8. *lucene索引_创建_域选项

    [索引建立步骤] [创建Directory] [创建writer] [创建文档并添加索引] 文档和域的概念很重要 文档相当于表中的每一条记录,域相当于表中的每一个字段. [查询索引的基本信息] 使用I ...

  9. Leetcode 152.乘机最大子序列

    乘积最大子序列 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 ...

  10. Journey CodeForces - 839C

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...