112 Chapter 7 Working with Arrays
As an example of the constattribute, the line
const double pi = 3.141592654;
declares the constvariable pi.This tells the compiler that this variable will not be modi-
fied by the program. If you subsequently wrote a line like this in your program:
pi = pi / 2;
the gcccompiler would give you a warning message similar to this:
foo.c:16: warning: assignment of read-only variable 'pi'
Returning to Program 7.7, the character array baseDigitsis set up to contain the 16
possible digits that will be displayed for the converted number. It is declared as a const
array because its contents will not be changed after it is initialized. Note that this fact
also aids in the program’s readability.
The array convertedNumberis defined to contain a maximum of 64 digits, which
holds the results of converting the largest possible longinteger to the smallest possible
base (base 2) on just about all machines.The variable numberToConvertis defined to be
of type long intso that relatively large numbers can be converted if desired. Finally, the
variables base(to contain the desired conversion base) and index(to index into the
convertedNumberarray) are both defined to be of type int.
After the user keys in the values of the number to be converted and the base—note
that the scanfcall to read in a longinteger value takes the format characters %ld—the
program then enters a doloop to perform the conversion.The dowas chosen so that at
least one digit appears in the convertedNumberarray even if the user keys in the num-
ber zero to be converted.
Inside the loop, the numberToConvertmodulo the baseis computed to determine
the next digit.This digit is stored inside the convertedNumberarray, and the indexin
the array is incremented by 1. After dividing the numberToConvertby the base, the con-
ditions of the doloop are checked. If the value of numberToConvertis 0 , the loop ter-
minates; otherwise, the loop is repeated to determine the next digit of the converted
number.
When the doloop is complete, the value of the variable indexis the number of digits
in the converted number. Because this variable is incremented one time too many inside
the doloop, its value is initially decremented by 1 in the forloop.The purpose of this
forloop is to display the converted number at the terminal.The forloop sequences
through the convertedNumberarray in reversesequence to display the digits in the cor-
rect order.
Each digit from the convertedNumberarray is in turn assigned to the variable
nextDigit.For the numbers 10 through 15 to be correctly displayed using the letters A
through F, a lookup is then made inside the array baseDigits, using the value of
nextDigitas the index. For the digits 0 through 9, the corresponding location in the
array baseDigitscontains nothing more than the characters '0'through '9'(which as
you recall aredistinct from the integers 0 through 9). Locations 10 through 15 of the
array contain the characters 'A'through 'F'.So, if the value of nextDigitis 10, for