Книга: Advanced PIC Microcontroller Projects in C

3.1.8 Constants

3.1.8 Constants

Constants represent fixed values (numeric or character) in programs that cannot be changed. Constants are stored in the flash program memory of the PIC microcontroller, thus not wasting valuable and limited RAM memory. In mikroC, constants can be integers, floating points, characters, strings, or enumerated types.

Integer Constants

Integer constants can be decimal, hexadecimal, octal, or binary. The data type of a constant is derived by the compiler from its value. But suffixes can be used to change the type of a constant.

In Table 3.2 we saw that decimal constants can have values from –2147483648 to +4294967295. For example, constant number 210 is stored as an unsigned char (or unsigned short int). Similarly, constant number –200 is stored as a signed int.

Using the suffix u or U forces the constant to be unsigned. Using the suffix L or l forces the constant to be long. Using both U (or u) and L (or l) forces the constant to be unsigned long.

Constants are declared using the keyword const and are stored in the flash program memory of the PIC microcontroller, thus not wasting valuable RAM space. In the following example, constant MAX is declared as 100 and is stored in the flash program memory of the PIC microcontroller:

const MAX = 100;

Hexadecimal constants start with characters 0x or 0X and may contain numeric data 0 to 9 and hexadecimal characters A to F. In the following example, constant TOTAL is given the hexadecimal value FF:

const TOTAL = 0xFF;

Octal constants have a zero at the beginning of the number and may contain numeric data 0 to 7. In the following example, constant CNT is given octal value 17:

const CNT = 017;

Binary constant numbers start with 0b or 0B and may contain only 0 or 1. In the following example a constant named Min is declared as having the binary value 11110000:

const Min = 0b11110000

Floating Point Constants

Floating point constant numbers have integer parts, a dot, a fractional part, and an optional e or E followed by a signed integer exponent. In the following example, a constant named TEMP is declared as having the fractional value 37.50:

const TEMP = 37.50

or

const TEMP = 3.750E1

Character Constants

A character constant is a character enclosed within single quote marks. In the following example, a constant named First_Alpha is declared as having the character value “A”:

const First_Alpha = 'A';

String Constants

String constants are fixed sequences of characters stored in the flash memory of the microcontroller. The string must both begin and terminate with a double quote character (“). The compiler automatically inserts a null character as a terminator. An example string constant is:

"This is an example string constant"

A string constant can be extended across a line boundary by using a backslash character (“”):

"This is first part of the string
and this is the continuation of the string"

This string constant declaration is the same as:

"This is first part of the string and this is the continuation of the string"

Enumerated Constants

Enumerated constants are integer type and are used to make a program easier to follow. In the following example, constant colors stores the names of colors. The first element is given the value 0:

enum colors {black, brown, red, orange, yellow, green, blue, gray, white};

Оглавление книги


Генерация: 0.034. Запросов К БД/Cache: 0 / 2
поделиться
Вверх Вниз