Выбрать главу

unsigned char total, sum;

sum = 150;

or

char total, sum;

sum = 150;

Variables can be assigned values during their declaration. Thus, the above statements can also be written as:

char total, sum = 150;

signed char or (signed) short (int)

The variables signed char, or (signed) short (int), are 8-bit signed character variables with a range of –128 to +127. In the following example a signed 8-bit variable named counter is created with a value of –50:

signed char counter = -50;

or

short counter = -50;

or

short int counter = -50;

(signed) int

Variables called (signed) int are 16-bit variables with a range –32768 to +32767. In the following example a signed integer named Big is created:

int Big;

unsigned (int)

Variables called (unsigned) int are 16-bit unsigned variables with a range 0 to 65535. In the following example an unsigned 16-bit variable named count is created and is assigned value 12000:

unsigned int count = 12000;

(signed) long (int)

Variables called (signed) long (int) are 32 bits long with a range –2147483648 to +2147483647. An example is:

signed long LargeNumber;

unsigned long (int)

Variables called (unsigned) long (int) are 32-bit unsigned variables having the range 0 to 4294967295. An example is:

unsigned long VeryLargeNumber;

float or double or long double

The variables called float or double or long double, are floating point variables implemented in mikroC using the Microchip AN575 32-bit format, which is IEEE 754 compliant. Floating point numbers range from ±1.17549435082E-38 to ±6.80564774407E38. In the following example, a floating point variable named area is created and assigned the value 12.235:

float area;

area = 12.235;

To avoid confusion during program development, specifying the sign of the variable (signed or unsigned) as well as the type of variable is recommended. For example, use unsigned char instead of char only, and unsigned int instead of unsigned only.

In this book we are using the following mikroC data types, which are easy to remember and also compatible with most other C compilers:

unsigned char 0 to 255
signed char –128 to 127
unsigned int 0 to 65535
signed int –32768 to 32767
unsigned long 0 to 4294967295
signed long –2147483648 to 2147483647
float ±1.17549435082E-38 to ±6.80564774407E38

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