In C, an integral type can declare:
The integral types are:
char
, signed char
,
unsigned char
-8 bits
short int
, signed short int
,
and unsigned short int
-16 bits
int
, signed int
, and
unsigned int
-32 bits
long int
, signed long int
,
and unsigned long int
-32 bits
(OpenVMS)
long int
, signed long int
, and
unsigned long int
-64 bits
(Digital UNIX)
signed __int64
(Alpha) and unsigned __int64
(Alpha)-64 bits
enum
-32 bits
For DEC C on OpenVMS systems, storage for int
and
long
is identical. Similarly, storage of signed
int
and signed long
is identical, and storage
for unsigned int
and unsigned long
is
identical.
For DEC C on Digital UNIX systems, storage for the int
data types is 32 bits, while storage for the long int
data types is 64 bits.
The 64-bit integral types signed __int64
and
unsigned __int64
are provided on Alpha processors.
For each of the signed integral types, there is a corresponding
unsigned integral type that uses the same amount of
storage. The unsigned
keyword with the integral type
modifies the way the integer value is interpreted, which allows
the storage of a larger range of positive values. When using the
unsigned
keyword, the bits are interpreted differently
to allow for the increased positive range with the unsigned type (at
the expense of the negative range of values). For example:
signed short int x = 45000; /* ERROR -- value too large for short int */ unsigned short int y = 45000;/* This value is OK */
The range of values for the signed short int
type is
-32,768 to 32,767. The range of values for the unsigned short
int
type is 0 to 65,535.
A computation involving unsigned operands can never overflow,
because any result outside the range of the unsigned
type is reduced to fit the type by the rules of modulus arithmetic.
If the result cannot be represented by the resulting integer type,
the result is reduced modulo the number that is one greater than
the largest value that can be represented by the resulting
unsigned integer type. This means that the low-order bits are kept,
and the high-order bits of the mathematical result that do not fit
in the type of the result are discarded. For example:
unsigned short int z = (99 * 99999); /* Value of y after evaluation is 3965 */
DEC C treats the plain char
type as signed
by default for compatibility with VAX C and many other C compilers.
However, a command-line option can control this, and a predefined
macro can be tested to determine the setting of the option in a
given compilation. On Alpha systems, unsigned char
might offer some performance advantage for character-intensive
processing.
An unsigned integer of n bits is always interpreted in straight unsigned binary notation, with possible values ranging from 0 to 2 n -1 .
Character types are declared with the keyword char
and
are integral types. Using char
objects for nonintegral
operations is not recommended, as the results are likely to be
nonportable. An object declared as a char
type can
always store the largest member of the source character set.
Valid character types are:
char
signed char
unsigned char
wchar_t
The wide character type wchar_t
is provided
to represent characters not included in the ASCII character set. The
wchar_t
type is defined using the typedef
keyword in the <stddef.h>
header file. Wide characters
used in constants or strings must be preceded with an L
. For example:
#include <stddef.h> wchar_t a[6] = L"Hello";
All char
objects are stored in 8
bits. All wchar_t
objects are stored as unsigned
int
objects in 32 bits. The value of a given character is
determined by the character set being used. In this text, the ASCII
character set is used in all examples. See Appendix C for a complete list of ASCII equivalents, in
decimal, octal, and hexadecimal radixes.
To aid portability, declare char
objects that will be
used in arithmetic as signed char
or unsigned
char
. For example:
signed char letter; unsigned char symbol_1, symbol_2; signed char alpha = 'A'; /* alpha is declared and initialized as 'A' */
Strings are arrays of characters terminated by the null character (\0). Section 1.8.3 has more information on the syntactic rules of using strings; Chapter 4 has information on declaring string literals.