## Introduction to Programming in C

Index   |   Using VC++

 1 Introduction 2 Basic Output 3 Variables 4 Conditionals 5 Loops 6 Example 1 7 More on Conditionals 8 Further Exploration 9 Algorithm Design 10 Arrays
< Previous

## Example 1

Next >
• Create a project called `Temperature`.
• Create a source file called `main.c`.
• Type in (or copy/paste) the following code:
• ```#include <stdio.h>

/* convert from Fahrenheit to Celsius using the formula
degrees C = (5/9) (degrees F - 32) */

#define fLast  300	// end with 300 degrees F
#define fStep   20	// step by 20 degrees F

int main()
{
int f;		// degrees Fahrenheit
int c;		// degrees Celsius

f = fFirst;
while (f <= fLast)
{
c = 5 * (f - 32) / 9;
printf("%d   %d\n", f, c);
f = f + fStep;
}

return 0;
}
```
• Check the Output window for errors.

### Explanations

• The `#define` lines at the top describe substitutions to be made by the compiler. Each time that `fStep` is used, the compiler will automatically use `20`. The effect is similar to when variables are initialized and used, but the `#define` method is quicker to execute. It is natural to wonder, "why not just put the numbers directly into the code?" That would work, but the list of defined values at the top makes the code easy to understand and modify (without sacrificing any efficiency).
• Notice that comments explain what the program will do and how the variables are used.
• Look at the calculation for degrees Celsius. The code doesn't use "`5/9 * (f-32)`" because the calculation `5/9` would result in a value of 0 since integer division is truncated. This is easily solved by doing the multiplication first, and then the division.

### Changes

Make the following changes to your program:

• Help the columns align a little better by separating the values with a tab ("`\t`") instead of spaces. Build, check for errors, and run with this change.
• Wouldn't it be nicer if the numbers were aligned on the right instead of the left? That's pretty simple to do! We can specify how many spaces each number should use, and it will be right-justified in that area. Three spaces will work nicely here. Change the string passed to `printf` to include a 3 in each %d:
`printf("%3d\t%3d\n", f, c);`
Build, check for errors, and run with this change.
• OK, that looks better, but it would be even more useful if we had better precision for the Celsius values. Let's use floating point numbers:
• First, we need to declare the variable `c` as a floating point variable. The common type for that is `double`, which is a double-precision floating point number that can store up to 15 significant digits. Change the declaration for `c` to:
`double c; // degrees Celsius`
• The calculation is still using integers, though (f, 5, 32, and 9 are all integer values). The first calculation will be (f-32). That's fine with integers, as is the multiplication by 5 that comes next. We just need to make sure to force a calculation using floating point numbers when we divide by 9. The rule is that if either operand is a floating point number, the calculation will be made with floating point. In this case, we can change "9" to "9.0" -- that makes it a floating point value.
• Note that we don't use floating point when it's not needed. There are several reasons for this -- integer math is faster, and integers require less memory.
• We need to change the call to `printf` to let it know that the second variable we're sending is a `double`. use %f to indicate a floating point number:
`printf("%3d\t%f\n", f, c);`
• Build, check for errors, and run with these changes.
• We can control the formatting of floating point numbers similarly to the way we did for integers. Try using "`%6.2f`" -- the 6 is the number of spaces that the value will use (including the decimal point); the 2 is the number of digits to show after the decimal.

### Exercises

• Add column headings. Think about where this code should go. Do you want it to happen each time through the loop? Before the loop? After it?
• Add another column (after the Celsius column) that will show "cold" for values under 70 degrees Celsius, or "warm" for values of 70 and over. Think about how you will decide what to print, and where this code should go.

When you are done working with this project, select File/Close Solution.

### Summary

• Use `#define` statements for constant values (instead of using variables or putting the numbers directly in the code). This helps code be more readable and easier to maintain.
• When making calculations, consider how integer division gets truncated. Use floating point numbers (`double` variables) when necessary.
• You can control formatting of numbers by using things like "`%3d`" and "`%6.2f`".