CPL - Chapter 6
Data Types
What is a data type? Data types are a collection of data values and set of pre-defined operations on those values. One major benefit of data typing is error catching. Another is documentation; we can tell a lot about about a program based on it’s data types and types used.
The two most common structured (non-scalar) data types are arrays and records. Structured data types defined w/ type operators, or constructors (C uses brackets for arrays, asterisks for pointers). Descriptors are the collection of the attributes of a variable.
Primitive Data Types
Data types that are not defined in terms of other types.
- Numeric Types:
- Integers: Different languages have different types, some common are;
short
,byte
,int
,long
- Floating-point: Models real numbers, but representations are only approximations for many real values;
float
ordoubles
- Complex: Ordered pairs of floating-point values
- Decimal: Mostly for larger computers designed to support business system applications; stores a fixed number of decimal digits, with the decimal point being fixed position in the value
- Integers: Different languages have different types, some common are;
- Boolean Types:
- Simplest of all types; one value for True, one for False, typically stored in a byte
- Character Types:
- Stored usually as numeric coding. Common coding’s are 8-bit or ASCII
Character String Types
A data type in which the values consist of sequences of characters. These have some inherent design issues;
- Should strings be a special kind of character array or a primitive type?
- Should strings have static or dynamic length?
Strings and Their Operators
Most common operations are assignment, concatenation, sub-string reference, comparison, & pattern matching. The sub-string reference is simply a reference to a sub-string of a given string.
If strings are not defined as a primitive type, strings are usually stored in arrays of single characters.
- C & C++ use
char
arrayschar str[] = 'apples';
- Equal to
apples0
where 0 is the null character that stops the list from being read past the characters
- Equal to
String Length Options
Three main options:
- Static Length String:
- The length can be static, set when the string is created. Such is the case for Python, Java, C++ Classes, etc.
- Descriptor:
- Static String (name of type)
- Length
- Address (of first character)
- Descriptor:
- The length can be static, set when the string is created. Such is the case for Python, Java, C++ Classes, etc.
- Limited Dynamic Length Strings:
- To allow strings to have varying length up to a declared and fixed maximum set by the variables definition
- Descriptor
- Limited Dynamic String
- Max length
- Current length
- Address
- Descriptor
- To allow strings to have varying length up to a declared and fixed maximum set by the variables definition
- Dynamic Length Strings:
- To allow strings to have varying length with no maximum. This is the case for JavaScript, Perl, and standard C++ libraries
Dealing with strings as arrays is overall more cumbersome than a primitive string type.
Enumeration Types
One in which all possible values, which are named constants, are provided, or enumerated, in the definition.
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}
There are some inherent design issues;
- Is an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant in the program checked?
- Are enumeration values coerced to integer?
- Are any other types coerced to an enumeration type?
All the above are type checking issues.
For languages without enumeration types, we can use int values; {red = 0; blue = 1;}
C++
enum colors {red, blue, green, yellow, black};
colors MyColor = blue, yourColor = red;
Because enumeration types evaluate to an integer value, they can be used in mathematical operations.
myColor++; // Setzs myColor to green from blue
myColor = 4; // Illegal in C++