Page 256 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 256
8.6 An Example 233
to indicate that it requires two arguments and returns one result, all of which are call-by
value-and-result 16-bit signed numbers. The compiler will use the prototype to convert
arguments of other types if possible. For instance, if x and y are 8-bit signed numbers
(of type char) then a call power (x,y) will automatically extend these 8-bit to 16-bit
signed numbers before passing them to the procedure. If a procedure has a return n
statement that returns a result, then the type statement in front of the procedure name
indicates the type of the result. If that type is declared to be void as in the puts { )
procedure, there may not be a return n statement that returns a result.
At the beginning of each file, prototypes for all procedures in that file should be
declared. While writing a procedure name and its arguments twice, once in a prototype
and later in the procedure itself, may appear clumsy, it lets the compiler check for
improper arguments and, where possible, instructs it to convert types used in the calling
routine to the types expected in the called routine. We recommend the use of prototypes.
The macro is similar to a procedure but is either evaluated at compile time or is
inserted into the program wherever it is used, rather than being stored in one place and
jumped to whenever it is called. The macro in C is implemented as a #def ine
construct. As #defines were earlier used to define constants, macros are also
"expanded" just before the program is compiled. The macro has a name and arguments
rather like a procedure, and the rest of the line is the body of the macro. For instance
#define f ( a, b, c) a = b * 2 + c
is a macro with name f and arguments a, b, and c. Wherever the name appears in
the program, the macro is expanded and its arguments are substituted. For instance if f {
x, y, 3) appeared, then x = y * 2 + 3 is inserted into the program. Macros with
constant arguments are evaluated at compile time, generating a constant used at run time.
8.6 An Example
A very nice coding scheme called the Huffman code can pack characters into a bit stream
and achieve about a 75% reduction in storage space when compared to storing the
characters directly in an ASCII character string. It can be used to store characters more
compactly and can also be used to transmit them through a communications link more
efficiently. As a bonus, the encoded characters are very hard to decode without a code
description, so you get a more secure communications link using a Huffman code.
Further, Huffman coding and decoding provide a rich set of examples of C techniques.
Figure 8.4. A Huffman Coding Tree