Page 31 - Programming Microcontrollers in C
P. 31

16     Chapter 1  Introduction to C

                          If these constants are used within a program, they must be identified
                          by quotes. In the earlier example, the new line character was a part of
                          a string. Therefore, it effectively was contained in quotes. If a single
                          character constant is to be generated, the constant must be included in
                          single quotes. For example, a test might include a statement like

                   if(c!=’\t’)
                       ....

                          This statement causes the variable c to be compared with the constant
                          ‘\t’, and the statement following the if will be executed if they are
                          not the same. Another preprocessor command is #define. With the
                          #define command, you can define a character sequence that will be
                          placed in your code sequence whenever it is encountered. If you have
                          character constants that you wish to use in your code, these constants
                          can be identified as
                   #define CR ‘\x0d’
                   #define LF ‘\x0a’
                   #define BELL ‘\x07’
                   #define NULL ‘\x00’
                          and so forth.
                              We’ll discuss the #define preprocessor command further later.
                          The following program shows use of an escape character.
                   /* Count lines of text in an input */

                   #include <stdio.h>


                   int main(void)
                   {
                       int c,nl=0; /* the number of lines is in nl */
                       while((c=getchar())!=EOF)
                          if(c==’\n’)
                              nl++;


                       printf(“The number of lines is %d\n”,nl);
                       return 0;
                   }
   26   27   28   29   30   31   32   33   34   35   36