Difference between revisions of "Machine Dependencies"
(One intermediate revision by the same user not shown) | |||
Line 12: | Line 12: | ||
compilers. | compilers. | ||
− | char | + | ;char |
:is at least 8 bits | :is at least 8 bits | ||
− | short | + | ;short |
:is at least 16 bits | :is at least 16 bits | ||
− | long | + | ;long |
:is at least 32 bits | :is at least 32 bits | ||
− | int | + | ;int |
:is the same as either short or long | :is the same as either short or long | ||
− | float | + | ;float |
:is at least 32 bits | :is at least 32 bits | ||
− | double | + | ;double |
:is at least as wide as float | :is at least as wide as float | ||
Line 44: | Line 44: | ||
file is only to be read by the same program then deleted. | file is only to be read by the same program then deleted. | ||
Different compilers use different amounts of padding between | Different compilers use different amounts of padding between | ||
− | structure members, though this can be modified via the | + | structure members, though this can be modified via the #pragma pack(n) construct. |
− | #pragma pack(n) construct. | ||
== Predefined Macros == | == Predefined Macros == | ||
Line 81: | Line 80: | ||
|TOS || Atari ST | |TOS || Atari ST | ||
|- | |- | ||
− | |||
|} | |} | ||
+ | ;Table 2. Predefined Macros | ||
Latest revision as of 18:17, 30 July 2017
HI-TECH C eliminates many of the machine dependent aspects of C, since it uniformly implements such features as unsigned char. There are however certain areas where machine dependencies are inherent in the C language; programmers should be aware of these and take them into account when writing portable code.
The most obvious of these machine dependencies is the varying size of C types; on some machines an int will be 16 bits, on others it may be 32 bits. HI-TECH C conforms to the following rules, which represent common practice in most C compilers.
- char
- is at least 8 bits
- short
- is at least 16 bits
- long
- is at least 32 bits
- int
- is the same as either short or long
- float
- is at least 32 bits
- double
- is at least as wide as float
Because of the variable width of an int, it is recommended that short or long be used wherever possible in
preference to int. The exception to this is where a quantity
is required to correspond to the natural word size of the
machine.
Another area of machine differences is that of byte ordering; the ordering of bytes in a short or long can vary significantly between machines. There is no easy approach to this problem other than to avoid code which depends on a particular ordering. In particular you should avoid writing out whole structures to a file (via fwrite()) unless the file is only to be read by the same program then deleted. Different compilers use different amounts of padding between structure members, though this can be modified via the #pragma pack(n) construct.
Predefined Macros
One technique through which machine unavoidable machine dependencies may be managed is the predefined macros provided by each compiler to identify the target processor and operating system (if any). These are defined by the compiler driver and may be tested with conditional compilation preprocessor directives.
The macros defined by various compilers are listed in table 2. These can be used as shown in the example in fig .
i8051 | 8051 processor family |
i8086 | 8086 processor family |
i8096 | 8096 processor family |
z80 | Z80 processor and derivatives |
m68000 | 68000 processor family |
m6800 | 6801, 68HC11 and 6301 processors |
m6809 | 6809 processor |
DOS | MS-DOS and PC-DOS |
CPM | CP/M-80 and CP/M-86 |
TOS | Atari ST |
- Table 2. Predefined Macros
#if DOS char * filename = "c:file"; #endif /* DOS */ #if CPM char * filename = "0:B:afile"; #endif /* CPM */