Thursday, 6 January 2011

The C Programming Language (K&R) 01x04—Convert Celsius to Fahrenheit With Constants (p. 17)

Another blog entry as I work my way through the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie aka K&R.

The reasons I’m doing it? To learn the language. To show others I know something about the C. To create notes for future reference where, on a blog, I can easily find an answer on something I did previously. To practice my writing of documentation and in general.

I am using Visual C++ 2010 and creating the code as a console application. At some point I will try other compilers, but that’s for another day.

This entry continues on the previous entry (here) where C code converted temperatures between Celsius and Fahrenheit.

K&R p. 17, Constants

The code in 01x03 used literals. The for loop started at 0 and went up to 300 in steps of 20. In looking at that code, it’s not self evident what these numbers represent. Enter the use of constants. Instead of using the literal of 300 for the upper limit, you define a constant UPPER and use UPPER instead of 300 in the code.

You would define the UPPER constant as follows:

#define UPPER 300

The definition occurs at the top of the file. There is no semicolon or equal sign.

When it comes time to compile your code, the compiler precompiles it but replacing every occurrence of UPPER with 300. In this way, the definition is not C code and the constant is not a variable. Declaring int UPPER = 300; is a variable and works differently.

The use of constants serves at least two purposes. It makes it easier to read the code and is thus considered good practice. Second, in the future you may have to change the constant.  It’s easier to make the change if you use a constant. You simply change the line where it’s defined. If you had used a literal in your code, you’d have to search through each occurrence, replace the number, hope you found them all and changed them correctly.

Like variables, a constant follows the same naming convention:

The name has the same form as a variable name: a sequence of letters and digits that begins with a letter.

Underscores are permissible in C and C++. The big caveat: you can’t name an identifier (variable, function, constant) with a digit as its first character. It must start with a letter or underscore.


Attempt #1

// Standard I/O library.
#include <cstdio>

// The standard library includes the system function.
#include <cstdlib>

// Constants.
#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */

int main()
{
     int fahr;

     for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
           printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

     // keep console windown open
     system("pause");

     // return some value
    return 0;
}

The output is the same as the 01x03 code:




Attempt #2

See what error messages pop up when using 1 to name LOWER.

#define 1LOWER 0 /* lower limit of table */
...
     for (fahr = 1LOWER; fahr <= UPPER; fahr = fahr + STEP)

It won’t compile.  All sorts of error messages.

error C2007: #define syntax
error C2059: syntax error : 'bad suffix on number'
error C2146: syntax error : missing ';' before identifier 'OWER'
error C2065: 'OWER' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'fahr'
warning C4552: '<=' : operator has no effect; expected operator with side-effect
error C2059: syntax error : ';'
error C2059: syntax error : ')'
error C2146: syntax error : missing ';' before identifier 'printf'


No comments:

Post a Comment