Книга: Advanced PIC Microcontroller Projects in C
3.1.7 Variable Types
Разделы на этой странице:
3.1.7 Variable Types
The mikroC language supports the variable types shown in Table 3.2. Examples of variables are given in this section.
Table 3.2: mikroC variable types
Type | Size (bits) | Range |
---|---|---|
unsigned char | 8 | 0 to 255 |
unsigned short int | 8 | 0 to 255 |
unsigned int | 16 | 0 to 65535 |
unsigned long int | 32 | 0 to 4294967295 |
signed char | 8 | –128 to 127 |
signed short int | 8 | –128 to 127 |
signed int | 16 | –32768 to 32767 |
signed long int | 32 | –2147483648 to 2147483647 |
float | 32 | ±1.17549435082E-38 to ±6.80564774407E38 |
double | 32 | ±1.17549435082E-38 to ±6.80564774407E38 |
long double | 32 | ±1.17549435082E-38 to ±6.80564774407E38 |
(unsigned) char or unsigned short (int)
The variables (unsigned) char, or unsigned short (int), are 8-bit unsigned variables with a range of 0 to 255. In the following example two 8-bit variables named total and sum are created, and sum is assigned decimal value 150:
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.1 Comments
- 3.1.2 Beginning and Ending of a Program
- 3.1.3 Terminating Program Statements
- 3.1.4 White Spaces
- 3.1.5 Case Sensitivity
- 3.1.6 Variable Names
- 3.1.7 Variable Types
- 3.1.8 Constants
- 3.1.9 Escape Sequences
- 3.1.10 Static Variables
- 3.1.11 External Variables
- 3.1.12 Volatile Variables
- 3.1.13 Enumerated Variables
- 3.1.14 Arrays
- 3.1.15 Pointers
- 3.1.16 Structures
- 3.1.17 Unions
- 3.1.18 Operators in C
- 3.1.19 Modifying the Flow of Control
- 3.1.20 Mixing mikroC with Assembly Language Statements
- Perl Variables and Data Structures
- Perl Variable Types
- Variables
- Assigning a Value to a Variable
- Accessing Variable Values
- Mixing Different Parameter Types
- 3.1.6 Variable Names
- 8.5.2 Typical Condition Variable Operations
- Appendix C. ICMP types
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Access Variable Data Files in the
- Using Environment Variables