3. Data Types and Sizes
|
| |
| 3.1 Numeric Data Types |
| |
| C has a small family of datatypes: Numeric (int, float, double), Character (char), and User defined (struct, union) |
| |
| Numeric Data Types: Depending on the precision and range required, you can use one of the following datatypes. |
| data type |
size
(Win7 64bits) |
signed |
unsigned |
| char |
1 |
char x;
signed char x; |
unsigned char x; |
| short |
2 |
short int x;
short y; |
unsigned short x;
unsigned short int y; |
| default |
4 |
int x; |
unsigned int x; |
| long |
4 |
long x; |
unsigned long x; |
| float |
4 |
float x; |
N/A |
| double |
8 |
double x; |
N/A |
|
| |
The unsigned version has roughly double the range of its signed counterparts.
Signed and unsigned characters differ only when used in arithmetic expressions.
The individual sizes are machine/compiler dependent. However, the following is guaranteed: |
sizeof(char)<sizeof(short)<=sizeof(int)<=sizeof(long)
sizeof(char)<sizeof(short)<=sizeof(float)<=sizeof(double) |
| |
| 3.2 Big endian vs. little endian |
| |
NUXI problem |
– This terminology alludes to the issue that a value represented by the byte-string "UNIX" on a big-
endian system may be stored as "NUXI" on a PDP-11 middle-endian system.
– For numeric data types that span multiple bytes, the order of arrangement of the individual bytes is important. |
Depending on the device architecture, we have "big endian" and "little endian" formats. |
| |
| 3.2.1 Big endian |
| |
The most significant bits (MSBs) occupy the lower address. |
This representation is used in the power pc processor. |
Networks generally use big-endian order, and thus it is called network order. |
Atomic element size 8-bit, address increment 1-byte (octet) |
 |
Atomic element size 16-bit, |
 |
| |
 |
| |
| 3.2.2 Little endian |
| |
The least signficant bits (LSBs) occupy the lower address. |
This representation is used on all x86 compatible processors. |
Atomic element size 8-bit, address increment 1-byte (octet) |
 |
Atomic element size 16-bit, |
 |
| |
 |
| |