Page 73 - Programming Microcontrollers in C
P. 73

58     Chapter 1  Introduction to C

                          equal to x. Any of these tests can be used to determine if the esti­
                          mate has been through enough iterations to be accurate.
                              The macro definition abs(x) is used to test for the end of the
                          loop. You will note that the argument is evaluated three times for the
                          expansion of the macro. If we place a lot of calculation within the
                          argument of a macro definition, the expansion of the macro may
                          cause the code to calculate the argument to be repeated several times.
                          For this reason, the expression
                   c = x1 - x2;

                          is placed inside of the while loop, and the test to determine loop
                          termination uses abs(c).

                              At the end of a function, a return statement will cause the value
                          of the expression following the word return to be evaluated and
                          returned to the calling function. If this expression is not of the type
                          specified by the function prototype, it will be converted to the cor­
                          rect type prior to being returned to the calling function. The expression
                          following the return statement can be enclosed in parentheses or not.
                              Another example will show the use of static external variables.
                   /* Read in a string from the keyboard and print it
                   out in reverse order. */


                   #include <stdio.h>

                   #define null 0
                   /* some function prototypes */
                   void push(int);
                   int pull(void);

                   int main(void)
                   {
                       int c;
                       push(null);
                       while((c=getchar())!=’\n’)
                          push(c);


                       printf(“\n”);
                       while((c=pull())!=null)
   68   69   70   71   72   73   74   75   76   77   78