As far as the circuitry of a computer is concerned, there’s only one kind of data—a series of bits (binary digits) fill-ing a series of memory locations. How those bits are to be interpreted by the people using the computer is entirely arbitrary. The purpose of data types is to define useful con-cepts such as integer, floating-point number, or character in terms of how they are stored in computer memory.
Thus, most computer languages have a data type called integer, which represents a whole number that can be stored in 16 bits (two bytes) of memory. When a programmer writes a declaration such as:
int Counter;
in the C language, the compiler will create machine instruc-tions that set aside two bytes of memory to hold the con-tents of the variable Counter. If a later statement says:
Counter = Counter + 1;
(or its equivalent, Counter++) the program’s instructions are set up to fetch two bytes of memory to the processor’s accumulator, add 1, and store the result back into the two memory bytes.
Similarly, the data type long represents four bytes (32 bits) worth of binary digits, while the data type float stores a floating-point number that can have a whole part and a decimal fraction part (see numeric data). The char (char-acter) type typically uses only a single byte (8 bits), which is enough to hold the basic ASCII character codes up to 255 (see characters and strings).
The Bool (Boolean) data type represents a simple true or false (usually 1 or 0) value (see Boolean operators).
Structured Data Types
The preceding data types all hold single values. However, most modern languages allow for the construction of data types that can hold more than one piece of data. The array is the most basic structured data type; it represents a series of memory locations that hold data of one of the basic types. Thus, in Pascal an array of integer holds integers, each taking up two bytes of memory.
Many languages have composite data types that can hold data of several different basic types. For example, the struct in C or the record in Pascal can hold data such as a person’s first and last name, three lines of address (all arrays of characters, or strings), an employee number (perhaps an integer or double), a Boolean field representing the presence or absence of some status, and so on. This kind of data type is also called a user-defined data type because programmers can define and use these types in almost the same ways as they use the language’s built-in basic types.
What is the difference between data types and data structures? There is no hard-and-fast distinction. Gen-erally, data structures such as lists, stacks, queues, and trees are more complex than simple data types, because they include data relationships and special functions (such as pushing or popping data on a stack). However, a list is the fundamental data type in list-processing lan-guages such as Lisp, and string operators are built into languages such as Snobol. (See list processing, stack, queue, and tree.) Further, in many modern languages fundamental and structured data types are combined seamlessly into classes that combine data structures with the relevant operations (see class and object-oriented programming).
No comments:
Post a Comment