Page 247 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 247

224                                    Chapter 8 Programming in C and C++

        data. More than one variable can be put in a declaration; the variables are separated by
        commas (,). A vector having n elements is denoted by the name and square brackets
        around the number of elements «, and the elements are numbered 0 to n.— 1, For
        example, the declaration int a, b[ 10 ]; shows two variables, a scalar variable a and a
        vector b with ten elements. Variables declared outside the procedure (e.g., before the line
        with procedure_name) are global, and those declared within a procedure (e.g., between
        the curly brackets { and } after procedure_name) are local. Parameters will be discussed
        in §8.5. A cast redefines a value's type. A cast is put in parentheses before the value. If
         i is an int, (char) i is a char.
             declaration of global variable;
             declaration of global variable;
             procedure_name (parameter_l, Parameter^,...)
             {
                     declaration of local variable;
                     declaration of local variable;
                     statement;
                     statement;

             }
                     Table 8.1. Conventional C Ooerators Used in Exoressions

















            Statements may be algebraic expressions that generate assembly-language
        instructions to execute the procedure's activities. A statement may be replaced by a
        sequence of statements within a pair of curly brackets ({ and }). This will be useful in
        conditional and loop statements discussed soon. Operators used in statements include
        addition, subtraction, multiplication, and division, and a number of very useful operators
        that convert efficiently to assembly-language instructions or program segments. Table
        8.1 shows the conventional C operators that we will use in this book. Although they are
        not all necessary, we use a lot of parentheses so we will not have to learn the precedence
        rules of C grammar. The following simple C procedure fun has (signed) 16-bit input
        parameter a and 32-bit local variable b; it puts 1 into b and then puts the (a+b) th
        element of the ten-element unsigned global 8-bit vector d into 8-bit unsigned global c
        and returns nothing (void) as is indicated by the data type to the left of the procedure
        name:
   242   243   244   245   246   247   248   249   250   251   252