Objects First |
A byte is the smallest directly addressable unit of memory in most computers and usually consists of 8 binary bits. A byte is sufficient to store one character encoded using the ASCII codes. (ASCII codes range from 0-255 or 0 - 28-1, requiring 8 binary bits.) Processors are very commonly classified by the basic word length, eg 8-, 16-, 32- or (most recently) 64-bit machines. Today, the most common machines are 32-bit ones. This means that the basic 'word' of the machine, used for storing basic numeric quantities has 32 bits.
If this word is used to store an unsigned integer,
then the values stored can range from 0 to 232-1
(0 to 4294967295).
C allows us to qualify int
and char types
to be unsigned.
Simply write the keyword unsigned
in front of the
int
or char:
unsigned int k, l, m;
unsigned char a, b, c;
If unsigned is used without the basic type name,
then C assumes you meant int.
Thus:
unsigned k, l, m;
is legal C and is identical to the first line of the previous
example.
The int type is signed,
ie values can be positive or negative.
On a 32-bit machine,
an int variable will usually require 32-bits of storage
and use the 2's complement
representation for signed numbers.
This implies a range of values from
-231 to +231-1.
Characters occupy 8-bit bytes.
The are 4 8-bit bytes in a 32-bit word.
Since most modern machines are byte-addressable,
ie each byte of memory is individually addressable.
An int
variable requires 32-bits or 4 bytes.
Thus addresses of successive int
variables differ by 4.
Note that because machines don't always implement the IEEE 754 standard
(eg IBM mainframes, DEC VAX),
the range and precision of C float and double may
vary!
From this, it is easily seen that the following are all acceptable
floating point constants:
Implementation limits
Technology is changing: we are moving towards machines with 64-bit
words.
Already some machines use a 64-bit word for int.
To cope with this, the ANSI standard requires that characterics
of any implementation be specified in
limits.h.
This contains such as
INT_MAX which is set to the
largest int on your machine (typically +2147483647 or
231-1).
Consult a textbook for the full list of implementation specific
constants.
Floating Point
Used for representing all non-integral or real values.
IEEE 754
Before the establishment of this standard,
machine manufacturers were free to implement floating point
variables in whatever way they pleased.
IEEE 754 defines formats for 32- and 64-bit floating point
values.
Consult an architecture text for the full story,
but for general programming purposes:
Size
(bits)Usual C
implementationSign
(bits)
Exponent
(bits)
Significand
/Mantissa
(bits)
Range Smallest absolute
valuePrecision Floating point constants
The following railroad diagram defines legal floating point constants:
1. 1.2 1e10 1.2e10
.002 1e+1 1.e-10 12.e12
Floating point methods (operations)
The usual arithmetic operations, +, -, * and /, are defined for
float
and double - as for integers.
Key terms |
| Continue on to Arrays | Back to the Table of Contents |