Page 57 - Programming Microcontrollers in C
P. 57

42     Chapter 1  Introduction to C

                              To reverse the data, the program starts by decrementing i so that
                          s[i], the last entry in the array, is the most significant character in
                          the number, and it must be placed in the first array location s[0]: .
                          Likewise, the character in s[0] must be placed in s[i]: and so
                          forth. The while loop that follows accomplishes this requirement.

                          EXERCISES

                          1. Write a program atoi(s[]) that starts with a character string
                            and converts this string to an int. Assume that the string contains
                            no sign.
                          2. Write a program that reads a text file one character at a time and
                            counts the number of words in the file.


            The If/Else Statement
                              The if/else statement has the general form

                   if(expression)
                       statement1;
                   else
                       statement2;

                          If the logical evaluation of the expression that is the argument of the
                          if is TRUE, statement1 will be executed. After statement1
                          is executed, program control will pass to the statement following
                          statement2, and statement2 will not be executed. If the evalu­
                          ation of statement is FALSE, statement2 will be executed, and
                          statement1 will be skipped. The else statement is not neces­
                          sary. If there is no else statement, the expression is evaluated. If it
                          is TRUE, statement1 will be executed. Otherwise, statement1
                          will be skipped. The following program demonstrates the use of the
                          if/else flow control method.

                   /* count number of digits and other characters in
                   input */

                   #include <stdio.h>


                   int main(void)
   52   53   54   55   56   57   58   59   60   61   62