我的主力博客:半亩方塘

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. python 2 encode and decode

    https://docs.python.org/2/howto/unicode.html a Unicode string is a sequence of code points, which ar ...

  2. JAVA基础——生产者消费者问题

    1.生产者消费者问题:经典案例 生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费 ...

  3. 根据数据库表自动生成实体类、xml和dao---mybatis

    网盘链接: https://pan.baidu.com/s/1AVGz0bDa_Y5zjk7vXa2eHw 提取码: 2gr6 1.记事本打开generatorConfig.xml文件 2(1,2,3 ...

  4. 诊断:AWR快照停止自动采集

    11.2.0.4数据库中,MMON进程,有时候由于一些莫名其妙的原因挂掉,接下来AWR的快照也就无法正常自动生成.MMON进程应该自动重启,却并没有自动被启动. 那么我们有可能是遇到了bug Bug ...

  5. Codeforces Beta Round #1 补题题解

    A Theatre Square(数学) 算出每行能装多少乘以每列能装多少就行 公式 ans=ceil(n/a)+ceil(m/a) 代码 #include <bits/stdc++.h> ...

  6. Linux系统安装Apache

    一,Apache和tomcat的区别与联系 apache是web服务器,web服务器专门处理http请求: tomcat是运行在apache上的应用服务器: apache是普通服务器,本身只支持htm ...

  7. Linux系统用户、组和权限管理

    一.用户与组 1.用户与组的概念 在linux系统中,根据系统管理需要将用户分为三种类型: 1.超级用户:root是linux系统的超级用户,对系统拥有绝对权限.由于root用户权限太大,只有在进行系 ...

  8. jsp获取绝对路径----${pageContext.request.contextPath}

    JSP取得绝对路径 在JavaWeb开发中,常使用绝对路径的方式来引入JavaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法如下: 一.使用${pageCont ...

  9. java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名

    类名.class.getName()的作用是获取这个类的全类名

  10. HDU 5458 Stability

    Stability Time Limit: 2000ms Memory Limit: 102400KB This problem will be judged on HDU. Original ID: ...