home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / GENCTXT.ZIP / CHAP6.TXT < prev    next >
Text File  |  1987-11-21  |  10KB  |  257 lines

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