home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP6.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  6.9 KB  |  191 lines

  1.                        Chapter 6 - Defines and Macros
  2.  
  3.  
  4.               DEFINES AND MACROS ARE AIDS TO CLEAR PROGRAMMING
  5.  
  6.              Load and display the file named DEFINE.C for your first 
  7.  
  8.         look  at  some defines and macros.   Notice the  first  four 
  9.  
  10.         lines  of the program each starting with the word "#define".  
  11.  
  12.         This is the way all defines and macros are defined.   Before 
  13.  
  14.         the actual compilation starts,  the compiler goes through  a 
  15.  
  16.         preprocessor  pass  to resolve all of the defines.   In  the 
  17.  
  18.         present case,  it will find every place in the program where 
  19.  
  20.         the combination "START" is found and it will simply  replace 
  21.  
  22.         it  with the 0 since that is the definition.   The  compiler 
  23.  
  24.         itself  will  never see the word "START",  so as far as  the 
  25.  
  26.         compiler  is concerned,  the zeros were  always  there.   It 
  27.  
  28.         should  be clear to you by now that putting the word "START" 
  29.  
  30.         in  your  program  instead  of  the  numeral  0  is  only  a 
  31.  
  32.         convenience  to you and actually acts like a  comment  since 
  33.  
  34.         the  word  "START" helps you to understand what the zero  is 
  35.  
  36.         used for.
  37.  
  38.              In  the  case of a very small  program,  such  as  that 
  39.  
  40.         before  you,  it  doesn't really matter what you  use.   If, 
  41.  
  42.         however,  you  had  a 2000 line program before you  with  27 
  43.  
  44.         references to the START,  it would be a completely different 
  45.  
  46.         matter.   If  you wanted to change all of the STARTs in  the 
  47.  
  48.         program  to a new number,  it would be simple to change  the 
  49.  
  50.         one  #define,  but difficult to find and change all  of  the 
  51.  
  52.         references  to it manually,  and possibly disastrous if  you 
  53.  
  54.         missed one or two of the references.
  55.  
  56.              In  the  same manner,  the preprocessor will  find  all 
  57.  
  58.         occurrences of the word "ENDING" and change them to 9,  then 
  59.  
  60.         the  compiler  will  operate  on the changed  file  with  no 
  61.  
  62.         knowledge that "ENDING" ever existed.
  63.  
  64.              It is a fairly common practice in C programming to  use 
  65.  
  66.         all  capital letters for a symbolic constant such as "START" 
  67.  
  68.         and  "ENDING"  and use all lower case letters  for  variable 
  69.  
  70.         names.  You can use any method you choose since it is mostly 
  71.  
  72.         a matter of personal taste.
  73.          
  74.                            IS THIS REALLY USEFUL?
  75.  
  76.              When  we  get  to the  chapters  discussing  input  and 
  77.  
  78.         output,  we  will need an indicator to tell us when we reach 
  79.  
  80.         the end-of-file of an input file.  Since different compilers 
  81.  
  82.         use different numerical values for this,  although most  use 
  83.  
  84.         either a zero or a minus 1, we will write the program with a 
  85.  
  86.         "define"  to define the EOF used by our particular compiler.  
  87.  
  88.         If at some later date,  we change to a new compiler, it is a 
  89.  
  90.         simple matter to change this one "define" to fix the  entire 
  91.  
  92.         program.   End-of-line  is  another  indicator that  is  not 
  93.  
  94.  
  95.  
  96.                                   Page 39
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                        Chapter 6 - Defines and Macros
  107.  
  108.  
  109.         universal.   This  will make more sense when we get  to  the 
  110.  
  111.         chapters on input and output.
  112.  
  113.                               WHAT IS A MACRO?
  114.  
  115.              A macro is nothing more than another define,  but since 
  116.  
  117.         it  is capable of at least appearing to perform some logical 
  118.  
  119.         decisions  or  some math functions,  it has a  unique  name.  
  120.  
  121.         Consider the third line of the program on your screen for an 
  122.  
  123.         example of a macro.   In this case, anytime the preprocessor 
  124.  
  125.         finds the word "MAX" followed by a group in parentheses,  it 
  126.  
  127.         expects  to find two terms in the parentheses and will do  a 
  128.  
  129.         replacement of the terms into the second  definition.   Thus 
  130.  
  131.         the  first  term  will  replace  every  "A"  in  the  second 
  132.  
  133.         definition and the second term will replace every "B" in the 
  134.  
  135.         second definition.   When line 12 of the program is reached, 
  136.  
  137.         "index" will be substituted for every "A",  and "count" will 
  138.  
  139.         be  substituted  for  every "B".   Remembering  the  cryptic 
  140.  
  141.         construct  we studied a couple of chapters ago  will  reveal 
  142.  
  143.         that  "mx"  will  receive the maximum value  of  "index"  or 
  144.  
  145.         "count".   In  like manner,  the "MIN" macro will result  in 
  146.  
  147.         "mn" receiving the minimum value of "index" or "count".  The 
  148.  
  149.         results are then printed out.   There are a lot of seemingly 
  150.  
  151.         extra  parentheses in the macro definition but they are  not 
  152.  
  153.         extra,  they  are  essential.   We  will discuss  the  extra 
  154.  
  155.         parentheses in our next program. 
  156.  
  157.              Compile and run DEFINE.C.
  158.  
  159.                          LETS LOOK AT A WRONG MACRO
  160.  
  161.              Load  the  file named MACRO.C and display  it  on  your 
  162.  
  163.         screen for a better look at a macro and its use.   The first 
  164.  
  165.         line  defines a macro named "WRONG" that appears to get  the 
  166.  
  167.         cube of "A",  and indeed it does in some cases, but it fails 
  168.  
  169.         miserably in others.  The second macro named "CUBE" actually 
  170.  
  171.         does get the cube in all cases. 
  172.  
  173.              Consider  the program itself where the CUBE of i+offset 
  174.  
  175.         is  calculated.   If  i is 1,  which it is  the  first  time 
  176.  
  177.         through,  then  we will be looking for the cube of 1+5 =  6, 
  178.  
  179.         which  will result in 216.  When using "CUBE",  we group the 
  180.  
  181.         values like this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216.  However, 
  182.  
  183.         when we use WRONG,  we group them as 1+5*1+5*1+5 = 1+5+5+5 = 
  184.  
  185.         16 which is a wrong answer.   The parentheses are  therefore 
  186.  
  187.         required  to  properly  group the  variables  together.   It 
  188.  
  189.         should  be clear to you that either "CUBE" or "WRONG"  would 
  190.  
  191.         arrive  at  a correct answer for a single  term  replacement 
  192.  
  193.         such as we did in the last program.   The correct values  of 
  194.  
  195.         the  cube  and the square of the numbers are printed out  as 
  196.  
  197.         well as the wrong values for your inspection. 
  198.  
  199.  
  200.  
  201.                                   Page 40
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.                        Chapter 6 - Defines and Macros
  212.  
  213.  
  214.  
  215.              The remainder of the program is simple and will be left 
  216.  
  217.         to your inspection and understanding.
  218.  
  219.  
  220.         PROGRAMMING EXERCISE
  221.  
  222.         1.   Write a program to count from 7 to -5 by counting down. 
  223.  
  224.              Use #define statements to define the limits. (Hint, you 
  225.  
  226.              will  need to use a decrementing variable in the  third 
  227.  
  228.              part of the "for" loop control.
  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.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.                                   Page 41
  272.  
  273.