Page 20 - Programming Microcontrollers in C
P. 20

Some Simple Programs          5

                          notifies the compiler to generate code that will cause the integer stored
                          in location a to be multiplied by the integer in b and the result of that
                          product to be multiplied by the integer found in c. Usually, the name
                          a, b, or c is used to designate the content of the memory location
                          assigned to the label name. This integer result will be stored in the
                          location identified by d.
                              The print statement
                   printf(“a * b * c = %d\n”, d);

                          is similar to the same statement in the first example. In this case,
                          however, the data string
                   “a * b * c = %d\n”

                          contains a printer command character %d. This character notifies the
                          printf function that it is to take the first argument following the
                          data string, convert it to a decimal value, and print it out to the screen.
                          The result of this line of code will be

                   a * b * c = 100
                          printed on the screen.
                              The line of code

                   d=a*b+c;
                          demonstrates another characteristic of the language. Each operator
                          is assigned a precedence that determines the order in which an ex­
                          pression is evaluated. The parenthesis operators are of the highest
                          precedence. The precedence of the * operator is higher than that of
                          the + operator, so this expression will be evaluated as

                   d=(a*b)+c;
                              In other words, the product indicated by * will be executed prior
                          to the addition indicated by the +. The expression that follows later
                          in the code

                   d=a+b*c;
                          will be evaluated as

                   d=a+(b*c);
                          causing the result of the third calculation to differ from that of the
                          second.
   15   16   17   18   19   20   21   22   23   24   25