A program to print Fahrenheit-Celsius table with floating-point values
来源:互联网 发布:informatica windows 编辑:程序博客网 时间:2024/05/21 21:35
我的主力博客:半亩方塘
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;}
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;
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
- Fahrenheit-Celsius table
- [C1.2] Fahrenheit-Celsius Table
- Decimal to Floating-Point Conversions
- How can I convert a floating-point value to an integer in C?
- Floating Point Number In a Nutshell
- Show how to print a table in a html page
- write a program to produce a truth table.
- Adding Expressions, Default Values and Constraints to a Table
- Write a program to print the fibonacci series sum upto a particular no. using recursion.
- Write a C program to print all permutations of a given string
- Write a program to print all permutations of a given string
- Fahrenheit convert to centigrade
- Inotify Example: Introduction to Inotify with a C Program Example
- How to Create a High Availability Setup with Pacemaker, Corosync and Floating IPs on CentOS 7
- a weird result when I try to print the value of point by %p format
- non-floating-point argument in call to function ‘__builtin_isnan’
- Adding a Floating Panel to a Window
- 第二周作业
- Linux程序运行中加载动态库
- Android中RelativeLayout布局各个属性含义解释
- 为ie和chrome FF单独设置样式的“条件注释法”、“类内属性前缀法”、“选择器前缀法”、实现方法 案例(推荐)
- 秋天里的思念
- A program to print Fahrenheit-Celsius table with floating-point values
- POJ 3067 Japan(树状数组)
- 【线段树】 VK Cup 2015 - Qualification Round 1 D. Closest Equals
- php无限分类
- openCV学习笔记(4):绘制直方图和calcHist()函数、normalize()函数的解析
- (1.3.3)插入排序:直接插入、交换插入、折半插入
- Duilib学习笔记《01》— duilib整体框架认识
- CSS hack技巧大全 案例演示
- Number of 1 Bits