sorcerer_see: (Default)
PS I synthesize here in my own words what K&R teach, re-write all their example programs, character by character, solve all the C exercises they hand out. I comment to what K&R teach cursively. I do not copy-paste anything from the book! In regards to the percentages listed: books often have prefaces with Roman numbers and often also lengthy Appendices. In my calculation I include all pages that I effectively study and synthesize.

TCPL 2Ed - Page 012 to 013 - 6.41% Completion

k-r_prg003.c


k-r_prg003.out


There are only a few differences between the former version of the Fahrenheit to Celsius conversion program and this one:
  • fahr and celsius are decared as float instead of integer
float allows for fractional numbers, so that they are not truncated and therefor more accurate
  • the printout looks better because the numbers are equally aligned from the right
printf("%3.0f %6.1f\n", fahr, celsius);

%3.0f
says a floating-point number is to be printed at least 3 characters wide, without decimal point and no fraction digits,

%6.1f
says to print a number presenting Celsius that will be at least 6 characters wide, and having 1 digit after the comma

%.2f
says that width is not constrained (no character count given) and to print 2 characters after the decimal point. (%)f is saying to print the number as floating-point.

A few more examples:

%d = print as decimal integer
%6d = print as decimal integer, at least 6 characters wide
%f = print as floating-point
%6f = print as a floating point, at least 6 characters wide
%.2f = print as floating-point, with 2 characters after the decimal point
%6.2f = print as floating-point, at least 6 wide and with 2 characters after the decimal point

printf recognizes, among others,  %o for octal, %x for hexadecimal, %c for character, %s for character string and %% for % itself.
  • the formula is written in a more natural manner
it became possible to just write (5/9) and not needed to use a roundabout to avoid all zero's: 5 * (fahr - 
) / 9;

C mechanics:

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, the integer will be converted to floating-point before the operation is performed.
Had we written fahr-32, it would have been converted to 32.0. Even though 32.0 = 32 and thus is an integer, writing it like 32.0 makes it clear to human-readers that it is treated by the program as having a floating-point nature. So that readers know that decimal fractions are taken in account.

The C rules for the conversion of integers to floating-point is being spoken of in detail in Chapter 2.

As of now it is worth-while to notice that the int in

fahr = lower;

and 

while (fahr <= upper)

also gets converted to float before the operation is done.

Exercise 1-3
Modify the temperature conversion program to print a heading above the table.

k-r_exc1-3.c


k-r_exc1-3.out


Exercise 1-4
Write a program to print the corresponding Celsius to Fahrenheit table.

At first I added decimal digits to the Fahrenheit output but since none of the Celsius numbers listed has, when converted, a fractional Fahrenheit number as outcome, I omitted decimals, to make the program look better.

k-r_exc1-4.c


k-r_exc1-4.out

Profile

sorcerer_see: (Default)
sorcerer_see

October 2025

S M T W T F S
    12 34
56 7 8 9 10 11
12 1314 15 16 1718
19 2021 22 23 2425
2627 28293031 

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 16th, 2026 07:31 am
Powered by Dreamwidth Studios