Page 22 - Programming Microcontrollers in C
P. 22

Some Simple Programs          7

                          executed. The argument should be read “i is less than 11.” The ini­
                          tially assigned value for i was 1, so the argument is TRUE. The
                          compound statement

                   {
                       printf(“\t%d\t%d\t%d\n”, i, i*i,i*i*i);
                       i=i+1;
                   }

                          will start execution with the value of i being equal to 1. Once this
                          statement is evaluated, control is passed back to the while and its
                          argument is evaluated. If the argument is TRUE, the statement fol­
                          lowing will be evaluated again. This sequence will repeat until the
                          argument evaluates as FALSE.
                              In this expression, the string argument of the printf function
                          contains three %d commands. Each %d command causes the corre­
                          sponding argument following the string to be printed to the screen.
                          There are tab characters, \t, to separate the various printed values
                          on the screen. The first  %d  will cause the value of i to be printed
                                                                                      2
                          on the screen. The second  %d  will cause the value i*i,  or  i ,  to
                          be printed to the screen. The third  %d  will print the value of i*i*i,
                               3
                          or  i  to be printed. When C executes the function call, the values
                          of the arguments are calculated prior to the call, so arguments like
                          i*i  are evaluated by the calling program and passed by value to
                          the function.
                              The statement
                   i=i+1;

                          is an example of the use of both precedence and association—the
                          direction in which expressions are evaluated—in C. The equal sign
                          here is an operator just like the + symbol. The + operator is evaluated
                          from left to right, and the = operator is evaluated from right to left.
                          Also, the + operator has higher precedence than the = operator. There­
                          fore, the above statement will add one to the value stored in i and
                          then assign this new value to the variable i. This expression simply
                          increments the variable i.
                              The above statement is the terminating statement of the com­
                          pound statement following the while. Since i had an initial value
                          of 1, control will be returned to the while with a value of 2 for  i.  2,
   17   18   19   20   21   22   23   24   25   26   27