Page 19 - Programming Microcontrollers in C
P. 19

4      Chapter 1  Introduction to C

                       d=a*b+c;
                       printf(“a * b + c = %d\n”, d);
                       d=a+b*c;
                       printf(“a + b * c = %d\n”, d);
                       return 0;
                   }
                              Before discussing this bit of code, we need to talk about the num­
                          bers used in it. Like most high-level languages, C provides for different
                          classes of numbers. These classes can each be variable types. One
                          class is the integer type and a second is the floating point type. We
                          will examine these number classes in more detail later, but for now
                          let us concentrate on the integer types. Integer numbers usually have
                          a numeric range of about ±2  (n-1) , where n is the number of bits that
                          contains the integer type. Integers are also called integral types. Inte­
                          gral types do not “understand” or permit fractions. Any fraction that
                          results from a division operation will be truncated and disappear from
                          the calculation. All variables must be declared or defined to be a
                          specific type prior to their use in a program.
                              The first line of code in main

                   int a,b,c,d;
                          declares the variables a, b, c, and d to be integer types. This par­
                          ticular statement is both a declaration and a definition statement. A
                          definition statement causes memory to be allocated for each vari­
                          able, and a label name to be assigned each location. A declaration
                          statement does not cause memory allocation, but rather it merely
                          provides information as to the nature of the variable to the compiler.
                          We will see more of definition and declaration statements later.
                              The three assignment statements
                   a=10;
                   b=5;
                   c=2;
                          assign initial values to the variables a, b, and c. The equal sign
                          signifies assignment. The value 10 is placed in the memory location
                          designated as a, etc. The next statement

                   d=a*b*c;
   14   15   16   17   18   19   20   21   22   23   24