Page 26 - Programming Microcontrollers in C
P. 26
Types and Type Declarations 11
const double PI = 3.14159265;
will create the value for the mathematical constant pi and store it in
the location provided for PI. Any attempt to change the value of PI
by the program will cause compiler error.
Conventions for writing constants are straightforward. A simple
number with no decimal point is an int. To make a number long,
you must suffix it with an l or an L. For example, 6047 is an int and
6047L is a long. The u or U suffix on a number will cause creation
of a proper unsigned number.
A floating-point number must contain a decimal point or an ex
ponent or both. The numbers 1.114 and 17.3e-5 are examples of
floating point numbers. All floating point numbers are of the type
double unless a suffix is appended to the number. Any number
suffixed with an f or an F is a single precision floating-point num
ber, and a suffix of l or L on a floating-point number will generate a
type long double. Octal (base 8) and hexadecimal (base 16)
numbers can be created. Any number that is prefixed with a 0—a
leading zero—is taken to be an octal number. Hexadecimal numbers
are prefixed with a 0x or a 0X. The rules above for L and U also
apply to octal and hexadecimal numbers.
The final type qualifier is volatile. The qualifier volatile
instructs the compiler to NOT optimize any code involving the vari
able. In execution of an expression, a side effect refers to the fact that
the expression alters something. The side effect of the following state
ment
a=b+c;
is that the stored value of a is changed. A sequence point is a point in
the code where all side effects of previous evaluations are completed
and no side effects from subsequent evaluations will have taken place.
An important consideration of the optimization is that if an expression
has no side effects, it can be eliminated by the compiler. Therefore, if
a statement involves no sequence point, or alters no memory, it is sub
ject to being discarded by the compiler. This operation is not particularly
bad when writing normal code, but when working with microcontrollers
where events can occur as a result of hardware operations, not the
program, this optimization can utterly destroy a program. For example,
whenever the hardware can alter a stored value, the compiler should