Page 33 - Programming Microcontrollers in C
P. 33

18     Chapter 1  Introduction to C

                          evaluation—either TRUE or FALSE—will be assigned to the vari­
                          able c. This result is of course incorrect. To avoid this problem, use
                   (c = getchar()) != EOF

                          as the while argument. In this case, the parentheses group the
                          c=getchar() expression and it will be completed prior to execu­
                          tion of the comparison. The variable c will have the correct value as
                          returned from the input stream. If the above expression is logically
                          true, then the value that was returned from the input stream is tested
                          to determine if it is a new line character. If a new line character is
                          found, the counter nl is incremented. Otherwise, the next character
                          is read in and the sequence repeated until an EOF is returned from
                          the getchar(). Whenever an assignment is executed inside of
                          another expression, always enclose the complete assignment expres­
                          sion in parentheses.
                              The final statement in the program
                   printf(“The number of lines is %d\n”,nl);

                          prints out the number of new line characters detected in reading the
                          input file.


            Arrays

                              An array is a collection of like types of data that are stored in
                          consecutive memory locations. An array is designated at declaration
                          time by appending a pair of square brackets to the array name. If the
                          size of the array is to be determined at the declaration, the square
                          brackets can contain the number of elements in the array. Following
                          are proper array declarations.
                   extern int a[];
                   long rd[100];
                   float temperatures[1000];
                   char st[]={“Make a character array”};
                   float pressure[]={ 1.1, 2.3, 3.9, 3.7, 2.5, 1.5,
                   0.4};

                          As you can see, the size of an array must be designated in some
                          manner before you can use empty square brackets in the designation.
                          In the first case above, the array a[] is defined in global memory, so
                          all that is necessary for the compiler to know is that a[] is an array.
   28   29   30   31   32   33   34   35   36   37   38