The type of a data object in C determines the range and kind of values an object can represent, the size of machine storage reserved for an object, and the operations allowed on an object. Functions also have types, and the function's return type and parameter types can be specified in the function's declaration.
The following sections discuss these topics:
void
type (Section 3.5)
The selection of a data type for a given object or function is one of the fundamental programming steps in any language. Each data object or function in the program must have a data type, assigned either explicitly or by default. (Chapter 4 discusses the assignment of a data type to an object.) C offers a wide variety of types. This diversity is a strong feature of C, but can be initially confusing.
To help avoid this confusion, remember that C has only a few
basic types. All other types are derived combinations of these
basic types. Some types can be specified in more than one way; for
example, short
and short int
are the same
type. (In this manual, the longest, most specific name is always
used.) Type is assigned to each object or function as part of the
declaration. Chapter 4
describes declarations in more detail.
Table 3-1 lists the basic data types: integral types (objects representing integers within a specific range), floating-point types (objects representing numbers with a significand part-a whole number plus a fractional number-and an optional exponential part), and character types (objects representing a printable character). Character types are stored as integers.
Integral Types | Floating Point Types |
---|---|
short int |
float |
signed short
int | double |
unsigned short int | long
double |
int
| |
signed int
| |
unsigned int
| |
long int
| |
signed long
int | |
unsigned
long int | |
Integral Character Types | |
char | |
signed char | |
unsigned char |
The integral and floating-point types combined are called the arithmetic types. See Section 3.1 for information about the size and range of integral and floating- point values.
A large variety of derived types can be created from the basic types. Section 3.4 discusses the derived types.
Besides the basic and derived types, there are three keywords that
specify unique types: void
, enum
, and
typedef
:
void
keyword specifies a special
type indicating no value, or it can be used with the pointer
operator (*) to indicate a generic pointer type. See Section 3.5 for more information on
the void
type.
enum
keyword specifies an integer type
of your own design, specifying the acceptable values of the type
to a predefined set of named integer constant values. Enumerated
types are stored as integers. See Section 3.6 for a detailed description of enumerated types.
typedef
keyword specifies a synonym
for a type made from one or more basic or derived types. See
Section 3.8 for more information on
creating type definitions.
There are also the type-qualifier keywords:
const
, used to prevent write access to an
object (see Section 3.7.1)
volatile
, used to restrict the optimizations
that might otherwise be performed on references to an object (see
Section 3.7.2)
__unaligned
(Alpha), used in pointer definitions, to indicate
to the compiler that the data pointed to is not properly aligned
on a correct address
__restrict
(for pointer type only), used
to designate a pointer as pointing to a distinct object,
thus allowing compiler optimizations to be made (see Section 3.7.4)
Using a qualifying keyword in the type declaration of an object results in a qualified type. See Section 3.7 for general information on type qualifiers.
With such a wide variety of types, operations in a program often need to be performed on objects of different types, and parameters of one type often need to be passed to functions expecting different parameter types. Because C stores different kinds of values in different ways, a conversion must be performed on at least one of the operands or arguments to convert the type of one operand or argument to match that of the other. You can perform conversions explicitly through casting, or implicitly through the compiler. See Section 6.10 for more information on data-type conversions. See Section 2.7 for a description of type compatibility.
See your platform-specific DEC C documentation for a description of any implementation-defined data types.