Page 59 - Programming Microcontrollers in C
P. 59

44     Chapter 1  Introduction to C

                          to the character 0. If it is, the result of this expression is TRUE. Oth­
                          erwise, the result is FALSE. The expression c<=’9' determines if
                          the input character is less than or equal to the character 9. If both of
                          these logical expressions are TRUE, then the AND of the two will be
                          TRUE, and the statement nn++ will be executed to count the num­
                          bers found in the input stream. Program control will then skip to the
                          end of the if statement and continue to execute the while loop. If,
                          on the other hand, either of these expressions is FALSE, then the
                          AND of the two results will be FALSE and the statement no++ will
                          be executed. This statement keeps count of the number of characters
                          that are not digits found in the input stream.
                              At the conclusion of the program, the getchar() will return
                          an EOF character and the program will fall out of the while loop. It
                          will then execute the following statement:

                   printf(“Digits=%d and other characters=%d\n”,nn,no);
                          The string contained within the double quotes in this argument causes
                          a combination of text plus calculated values of variables to be printed
                          out. Suppose that the program found 51 numbers and 488 other char­
                          acters. The printout from the program would then be:

                   Digits=51 and other characters=488
                          Each %d is associated with its corresponding argument and converted
                          to a numerical value before it is sent to the screen.

            The If-Else If Statement

                              Sometimes it is necessary to select among several alternatives.
                          One of the methods that C offers is the if-else if sequence.
                          Examine the following program that counts the number of occur­
                          rences of each vowel in an input. The program also counts all other
                          characters found in the input.
                   /* Count the number of occurrences of each vowel
                   found in an input and also count all other charac­
                   ters. */

                   #include <stdio.h>


                   int main(void)
   54   55   56   57   58   59   60   61   62   63   64