home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor1.zip / CHAP06.TXT < prev    next >
Text File  |  1989-11-10  |  9KB  |  233 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                     Chapter 6
  6.                                            DEFINES AND MACROS
  7.  
  8.  
  9.  
  10. AIDS TO CLEAR PROGRAMMING
  11. ____________________________________________________________
  12.  
  13. Load and display the file named DEFINE.C for   ==============
  14. your first look at some defines and macros.       DEFINE.C
  15. Notice lines 4 through 7 of the program, each  ==============
  16. starting with #define.  This is the way all
  17. defines and macros are defined.  Before the
  18. actual compilation starts, the compiler goes through a
  19. preprocessor pass to resolve all of the defines.  In the
  20. present case, it will find every place in the program where
  21. the combination START is found and it will replace it with the
  22. 0 since that is the definition.  The compiler itself will
  23. never see the word START, so as far as the compiler is
  24. concerned, the zeros were always there.  Note that if the
  25. string is found in a string constant or in a comment, it will
  26. not be changed.
  27.  
  28. It should be clear to you that putting the word START in your
  29. program instead of the numeral 0 is only a convenience to you
  30. and actually acts like a comment since the word START helps
  31. you to understand what the zero is used for.
  32.  
  33. In the case of a very small program, such as that before you,
  34. it doesn't really matter what you use.  If, however, you had
  35. a 2000 line program before you with 27 references to START,
  36. it would be a completely different matter.  If you wanted to
  37. change all of the STARTs in the program to a new number, it
  38. would be simple to change the one #define.  If this technique
  39. were not used, it would be difficult to find and change all
  40. of the references to it manually, and possibly disastrous if
  41. you missed one or two of the references.
  42.  
  43. In the same manner, the preprocessor will find all occurrences
  44. of the word ENDING and change them to 9, then the compiler
  45. will operate on the changed file with no knowledge that ENDING
  46. ever existed.
  47.  
  48. It is a fairly common practice in C programming to use all
  49. capital letters for a symbolic constant such as START and
  50. ENDING and use all lower case letters for variable names.  You
  51. can use any method you choose since it is mostly a matter of
  52. personal taste.
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.                                                      Page 6-1
  60.  
  61.                                Chapter 6 - Defines and Macros
  62.  
  63. IS THIS REALLY USEFUL?
  64. ____________________________________________________________
  65.  
  66. When we get to the chapters discussing input and output, we
  67. will need an indicator to tell us when we reach the
  68. end-of-file of an input file.  Since different compilers use
  69. different numerical values for this, although most use either
  70. a zero or a minus 1, we will write the program with a #define
  71. to define the EOF used by our particular compiler.  If at some
  72. later date, we change to a new compiler, it is a simple matter
  73. to change this one #define to fix the entire program.  In most
  74. C compilers, the EOF is defined in the STDIO.H file.  You can
  75. observe this for yourself by listing the STDIO.H file that was
  76. supplied with your compiler.
  77.  
  78.  
  79.  
  80. WHAT IS A MACRO?
  81. ____________________________________________________________
  82.  
  83. A macro is nothing more than another define, but since it is
  84. capable of at least appearing to perform some logical
  85. decisions or some math functions, it has a unique name. 
  86. Consider line 6 of the program on your screen for an example
  87. of a macro.  In this case, anytime the preprocessor finds the
  88. word MAX followed by a group in parentheses, it expects to
  89. find two terms in the parentheses and will do a replacement
  90. of the terms into the second part of the definition.  Thus the
  91. first term will replace every A in the second part of the
  92. definition and the second term will replace every B in the
  93. second part of the definition.  When line 15 of the program
  94. is reached, index will be substituted for every A, and count
  95. will be substituted for every B.  Once again, it must be
  96. stated that string constants and comments will not be
  97. affected.  Remembering the cryptic construct we studied a
  98. couple of chapters ago will reveal that mx will receive the
  99. maximum value of index or count.  In like manner, the MIN
  100. macro will result in mn receiving the minimum value of index
  101. or count.
  102.  
  103. When defining a macro, it is imperative that there is no space
  104. between the macro name and the opening parenthesis.  If there
  105. is a space, the compiler cannot determine that it is a macro,
  106. but will handle it like a simple substitution define
  107. statement.  The results are then printed out in line 17. 
  108. There are a lot of seemingly extra parentheses in the macro
  109. definition but they are not extra, they are essential.  We
  110. will discuss the extra parentheses in our next example
  111. program.  Be sure to compile and execute DEFINE.C before going
  112. on to the next program.
  113.  
  114.  
  115.  
  116.  
  117.  
  118.                                                      Page 6-2
  119.  
  120.                                Chapter 6 - Defines and Macros
  121.  
  122. LET'S LOOK AT A WRONG MACRO
  123. ____________________________________________________________
  124.  
  125. Load the file named MACRO.C and display it on   =============
  126. your screen for a better look at a macro and       MACRO.C
  127. its use.  Line 4 defines a macro named WRONG    =============
  128. that appears to get the cube of A, and indeed
  129. it does in some cases, but it fails miserably
  130. in others.  The second macro named CUBE actually does get the
  131. cube in all cases.
  132.  
  133. Consider the program itself where the CUBE of i+offset is
  134. calculated.  If i is 1, which it is the first time through,
  135. then we will be looking for the cube of 1+5 = 6, which will
  136. result in 216. When using CUBE, we group the values like this,
  137. (1+5)*(1+5)*(1+5) = 6*6*6 = 216.  However, when we use WRONG,
  138. we group them as 1+5*1+5*1+5 = 1+5+5+5 = 16 which is a wrong
  139. answer.  The parentheses are therefore required to properly
  140. group the variables together.  It should be clear to you that
  141. either CUBE or WRONG would arrive at a correct answer for a
  142. single term replacement such as we did in the last program. 
  143. The correct values of the cube and the square of the numbers
  144. are printed out as well as the wrong values for your
  145. inspection. 
  146.  
  147. In line 7 we define the macro ADD_WRONG according to the above
  148. rules but we still have a problem when we try to use the macro
  149. in line 25 and 26.  In line 26 when we say we want the program
  150. to calculate 5*ADD_WRONG(i) with i = 1, we get the result 5*1
  151. + 1 which evaluates to 5 + 1 or 6, and this is most assuredly
  152. not what we had in mind.  We really wanted the result to be
  153. 5*(1 + 1) = 5*2 = 10 which is the answer we get when we use
  154. the macro named ADD_RIGHT, because of the extra parentheses
  155. in the definition given in line 8.  A little time spent
  156. studying the program and the result will be worth your effort
  157. in understanding how to use macros.
  158.  
  159. In order to prevent the above problems, most experienced C
  160. programmers include parentheses around each variable in a
  161. macro and additional parentheses around the entire expression.
  162.  
  163. The remainder of the program is simple and will be left to
  164. your inspection and understanding.
  165.  
  166.  
  167. WHAT IS AN ENUMERATION VARIABLE?
  168. ____________________________________________________________
  169.  
  170. Load and display the program named ENUM.C for    ============
  171. an example of how to use the enum type              ENUM.C
  172. variable.  Line 6 contains the first enum        ============
  173. type variable named result which is a
  174. variable which can take on any of the values
  175. contained within the parentheses.  Actually the variable
  176.  
  177.                                                      Page 6-3
  178.  
  179.                                Chapter 6 - Defines and Macros
  180.  
  181. result is an int type variable but can be assigned any of the
  182. values defined for it.  The names within the parentheses are
  183. int type constants and can be used anywhere it is legal to use
  184. an int type constant.  The constant win is assigned the value
  185. of 0, tie the value 1, bye the value 2, etc.
  186.  
  187. In use, the variable named result is used just like any int
  188. variable would be used as can be seen by its use in the
  189. program.  The enum type of variable is intended to be used by
  190. you, the programmer, as a coding aid since you can use a
  191. constant named mon for control structures rather than the
  192. meaningless (at least to you) value of 1.  Notice that days
  193. is assigned the values of days of the week in the remainder
  194. of the program.  If you were to use a switch statement, it
  195. would be much more meaningful to use the labels sun, mon, etc,
  196. rather than the more awkward 0, 1, 2, etc.
  197.  
  198.  
  199. PROGRAMMING EXERCISE
  200. ____________________________________________________________
  201.  
  202. 1.   Write a program to count from 7 to -5 by counting down.
  203.      Use #define statements to define the limits. (Hint, you
  204.      will need to use a decrementing variable in the third
  205.      part of the for loop control.
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.                                                      Page 6-4
  233.