C's numeric variables fall into the following two main categories:
- Integer variables hold values that have no fractional part (that is, whole numbers only). Integer variables come in two flavors: signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0).
- Floating-point variables hold values that have a fractional part (that is, real numbers).
Table 3.2. C's numeric data types.
Variable Type | Keyword | Bytes Required | Range |
Character | char | 1 | -128 to 127 |
Integer | int | 2 | -32768 to 32767 |
Short integer | short | 2 | -32768 to 32767 |
Long integer | long | 4 | -2,147,483,648 to 2,147,438,647 |
Unsigned character | unsigned char | 1 | 0 to 255 |
Unsigned integer | unsigned int | 2 | 0 to 65535 |
Unsigned short integer | unsigned short | 2 | 0 to 65535 |
Unsigned long integer | unsigned long | 4 | 0 to 4,294,967,295 |
Single-precision | float | 4 | 1.2E-38 to |
floating-point | 3.4E381 | ||
Double-precision | double | 8 | 2.2E-308 to |
floating-point | 1.8E3082 | ||
1Approximate range; precision = 7 digits. | |||
2Approximate range; precision = 19 digits. |
Looking at Table 3.2, you might notice that the variable types int and short are identical. Why are two different types necessary? The int and short variable types are indeed identical on 16-bit IBM PC-compatible systems, but they might be different on other types of hardware. On a VAX system, a short and an int aren't the same size. Instead, a short is 2 bytes, whereas an int is 4. Remember that C is a flexible, portable language, so it provides different keywords for the two types. If you're working on a PC, you can use int and short interchangeably.
Tidak ada komentar:
Posting Komentar