Elementary Data Types

advertisement
CmSc315 Programming Languages
Chapter 5: Types, Part II
B. Scalar Data Types
1.
Basic Data Types
Scalar data types represent a single object, i.e. only one value can be derived.
In general, scalar objects follow the hardware architecture of a computer.
1.1. Numeric Data Types
1.1.1. Integers

Specification
Maximal and minimal values - depending on the hardware. In some languages
represented as a defined constant.
Operations:
Arithmetic
Relational
Assignment
Bit operations

Implementation
Most often using the hardware-defined integer storage representation and a set of hardware
arithmetic and relational primitive operations on integers.
1.1.2. Floating-point real numbers


Specification

Ordered sequence of some hardware-determined minimum negative value to a
maximum value.

Similar arithmetic, relational and assignment operations as with integers.
Roundoff issues - the check for equality may fail due to roundoff.
Implementation
Mantissa - exponent model. The storage is divided into a mantissa - the
significant bits of the number, and an exponent.
Example: 10.5 = 0.105 x 102,
Mantissa: 105, Exponent: 2
1.1.3. Fixed-point real numbers


Specification: Used to represent real numbers with predefined decimal places, such as
dollars and cents.
Implementation: May be directly supported by hardware or simulated by software.
1
1.2. Non-Numeric Data Types
1.2.1. Booleans

Specification
Two values: true and false. Can be given explicitly as enumeration, as in Pascal
and Ada. Basic operations: and, or, not.

Implementation
A single addressable unit such as byte or word.
Two approaches:
o
o
Use a particular bit for the value, e.g. the last bit; 1 - true, 0 -false.
Use the entire storage; a zero value would then be false, otherwise true.
1.2.2. Characters

Specification
Single character as a value of a data object.
Collating sequence - the ordering of the characters, used for lexicographic sorting.
Operations:
Relational
Assignment
Testing the type of the character - e.g. digit, letter, special symbol.

Implementation: Usually directly supported by the underlying hardware.
2. Non-Basic Types
2.1. Enumerations: Ordered list of different values
Example: enum StudentClass {Fresh, Soph, Junior, Senior}
the variable StudentClass may accept only one of the four listed values.
enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
enum day myDay = Wednesday;
In C/C++ the above values of this type are 0, ..., 6.
More powerful in Java:
for (day d : day.values())
Sytem.out.println(d);
Implementation: represented during run time as integers, corresponding to the
listed values.
2
2.2. Pointers
C, C++, Ada, Pascal
Value is a memory address
Indirect referencing
Operator in C: *
Example
struct Node
{
int key;
struct Node* next;
};
struct Node* head;
strcpy
void strcpy(char *p, char *q)
{
while (*p++ = *q++) ;
}
Pointer Operations
If T is a type and ref T is a pointer:
& : T → ref T
* : ref T → T
For an arbitrary variable x:
*(&x) = x
Equivalence between arrays and pointers
a = &a[0]
a[i] = *(a + i)
3
float sum(float a[ ], int n)
{
int i;
float s = 0.0;
for (i = 0; i<n; i++) s += a[i];
return s;
float sum(float *a, int n)
{
int i;
float s = 0.0;
for (i = 0; i<n; i++)
s += *a++;
return s;
4
Download