CSomputer
cience
Foundations
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 1 / 29
COS 140: Foundations of Computer Science
Variables and Primitive Data Types
Fall 2017
CSomputer
cience
Foundations
Homework
Introduction
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 2 / 29
• Reading: Chapter 16
• Homework: Exercises at end of chapter
• Homework due 10/23
CSomputer
cience
Foundations
What is a variable?
Introduction
• What is a variable?
• Variable attributes
• Binding
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 3 / 29
• In this lecture: imperative/OO languages
• A variable is an area of memory that:
◦ can have a value
◦ can have its value changed by the program
• Different sizes: byte, word, multiple words
CSomputer
cience
Foundations
Variable attributes
Introduction
• What is a variable?
• Variable attributes
• Binding
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 4 / 29
• Name – what do we call it?
• Address – where does it live in memory?
• Type – what kind of thing can it hold?
• Value – “contents” of the variable
• Scope – who can see this variable?
• Lifetime – duration of program, or shorter period?
CSomputer
cience
Foundations
Binding
Introduction
• What is a variable?
• Variable attributes
• Binding
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 5 / 29
• Associates an attriburte value with an attribute
• Not just “value” attribute
• Two types:
◦ Static binding: occurs before run-time, not changed afterward
• Allows compiler to check for errors, but...
• ...need information about the binding at compile time
◦ Dynamic binding: takes place/is changed at run-time
CSomputer
cience
Foundations
Variable names
Introduction
Variable names
• Variable names
• Name length
• Special names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 6 / 29
• Name: identifier associated variable
• Same name⇔ different variables in different contexts
• Different names⇔ same variable
• Unnamed variables: access via pointer
CSomputer
cience
Foundations
Variable names
Introduction
Variable names
• Variable names
• Name length
• Special names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 7 / 29
• Strings (symbols) in a symbol table
• Symbol tables created at compile/interpretation time
• Associate names with entities (e.g., variables)
CSomputer
cience
Foundations
Variable names
Introduction
Variable names
• Variable names
• Name length
• Special names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 8 / 29
• Which characters to allow?
• Have to recognize names during lexical analysis⇒ no spaces
• Sometimes special initial character – e.g., a letter
• For enhanced readability, may allow connectors (e.g., “-”, “ ”),
case-sensitivity
CSomputer
cience
Foundations
Length of variable names
Introduction
Variable names
• Variable names
• Name length
• Special names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 9 / 29
• Names should be meaningful⇒ longer names
• But symbol table should be reasonable size, easy to maintain⇒
shorter names
• BASIC: one letter + optional number
◦ Can’t complain about the size!
◦ Readable for mathematical applications, but not much else
• Limited number of characters – e.g., FORTRAN (6 or 31), C (63
significant)
◦ Trades-off readability and wasted space
◦ Table maintenance easy, but whatever the size, someone
always wants more
• Unlimited number of characters – e.g., Ada, Lisp
◦ Complete flexibility, but...
◦ ...potentially great deal of space, hard to maintain
CSomputer
cience
Foundations
Special names
Introduction
Variable names
• Variable names
• Name length
• Special names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 10 / 29
• Some names not allowed or limited
• Reserved words – can only be used in language-defined
context(s) – if in C, e.g.
• Predefined words – defined in the language, but can be changed
(e.g., Ada, C [libraries])
• Keywords – special meaning in some contexts, but can be used
as a name (e.g., for in Lisp, if in FORTRAN)
CSomputer
cience
Foundations
Giving variables values
Introduction
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 11 / 29
• Assignment construct binds a value to a variable
• Often, a construct such as: x = a+ b
◦ x’s value is bound to result of a+ b
◦ Not algebra: can have: x = x+ 1
CSomputer
cience
Foundations
Assignment construct examples
Introduction
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 12 / 29
C, Java, Python... x = x + 1;
Pascal x := x + 1
Lisp (setf x (1+ x))
R x <<- x + 1 or x + 1 -> x
Smalltalk x ← x + 1
Forth x 1 + x !
COBOL ADD X 1 GIVING X
TCL set x x + 1
CSomputer
cience
Foundations
Data types
Introduction
Variable names
Assignment
Data types
Primitive data types
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 13 / 29
• All data has a type: integer, floating point, etc.
• Type species size, interpretation, operations for data
• All variables have a type attribute
• Depending on language, variables’ type static or dynamic
• Determining variable type:
◦ Declared somewhere in program (static, explicit)
◦ Inferred by compiler/interpreter (static, implicit; dynamic)
CSomputer
cience
Foundations
What are primitive data types?
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 14 / 29
• Primitive data types: defined by language
• Not (usually) defined in terms of other types
• Building blocks of new types
• Unstructured/atomic data types (scalars):
◦ Integers
◦ Floating point numbers
◦ Booleans
◦ Characters
◦ Pointers
• Structured data types:
◦ Strings
◦ Arrays
◦ Records
CSomputer
cience
Foundations
Integers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 15 / 29
• Integer type represents subset of the integers Z (duh)
• Most common data type
• You’ve seen: sign-magnitude, two’s complement
• Most languages: several sizes (byte, 2 byte, 4 byte...)
◦ Can’t represent all possible integers in computer – instead
i . . . j, where i ≤ 0 ≤ j
◦ For two’s complement, with n bits, can represent
−2n−1 . . . 2n−1 − 1◦ E.g., 8 bits can represent 100000002 (= −12810) to
011111112 (= 12710).
• Some languages: unsigned integers (C)
CSomputer
cience
Foundations
Floating point numbers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 16 / 29
• Represent subset of real numbers R
• Usually two sizes
• Two parts: fraction and exponent
◦ E.g.: 0.11012, exponent 1012
CSomputer
cience
Foundations
Floating point numbers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 16 / 29
• Represent subset of real numbers R
• Usually two sizes
• Two parts: fraction and exponent
◦ E.g.: 0.11012, exponent 1012◦ Fractional part = 1
2 + 14 + 0
8 + 116 = 0.812510
CSomputer
cience
Foundations
Floating point numbers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 16 / 29
• Represent subset of real numbers R
• Usually two sizes
• Two parts: fraction and exponent
◦ E.g.: 0.11012, exponent 1012◦ Fractional part = 1
2 + 14 + 0
8 + 116 = 0.812510
◦ Exponent = 5, so multiply fraction by 25
CSomputer
cience
Foundations
Floating point numbers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 16 / 29
• Represent subset of real numbers R
• Usually two sizes
• Two parts: fraction and exponent
◦ E.g.: 0.11012, exponent 1012◦ Fractional part = 1
2 + 14 + 0
8 + 116 = 0.812510
◦ Exponent = 5, so multiply fraction by 25
◦ So number is 0.8125× 32 = 26.010
CSomputer
cience
Foundations
Floating point numbers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 16 / 29
• Represent subset of real numbers R
• Usually two sizes
• Two parts: fraction and exponent
◦ E.g.: 0.11012, exponent 1012◦ Fractional part = 1
2 + 14 + 0
8 + 116 = 0.812510
◦ Exponent = 5, so multiply fraction by 25
◦ So number is 0.8125× 32 = 26.010
• Can’t represent all numbers accurately – e.g., π, 13
CSomputer
cience
Foundations
Booleans
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 17 / 29
• True/false values
• Good for flags (switches), conditions to be checked
• Sometimes languages provide as primitive data type
• If not:
◦ 0 is false, everything else true (C)
◦ 0, ””, [], etc., is false, everything else is true (Python)
• Representation: smallest efficiently-addressable unit (often byte)
CSomputer
cience
Foundations
Characters
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 18 / 29
• Character represented as a pattern of 1s and 0s
• Representations:
◦ ASCII
• 7 bits code; 8-bit variants of this (e.g., ISO 8859–1) are the
most common codes used
• E.g.: “a” = 33, “b” = 34, ...; space = 32
◦ Unicode
• 16 bits (or longer); accommodates other alphabets and
symbols
• E.g.: - codepoints 47196, 51060; ;
◦ EBCDIC – 8 bits; old IBM code
CSomputer
cience
Foundations
Strings
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 19 / 29
• Sequence of characters: textual data
• Special operators (e.g., substring, concatenation), special
relational operators, sometimes special assignment
• Lengths:
◦ Fixed length: always same size
◦ Limited-dynamic – can change, but there’s a maximum (e.g.,
C)
• Length + sequence (often array) of characters
• Null-terminated sequence of characters
◦ Dynamic – can change without limit (e.g., Lisp) – more
flexible, but overhead for allocation/deallocation
CSomputer
cience
Foundations
Arrays
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 20 / 29
• Sometimes need to represent groups of related data items (e.g.,
integers, characters, etc.)
• An array is a data type that stores homogeneous data in
contiguous memory
• Can be single-dimensional (vectors) or multi-dimensional
• Use index to find element
• Value of index doesn’t affect time taken to find element⇒
random access storage
CSomputer
cience
Foundations
Design issues for Arrays
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 21 / 29
• Syntax for array indices – e.g., Temperatures(20) or
Temperatures[20]
• Subscripts:
◦ what is the lower bound?
◦ bounds checking or not?
◦ must subscripts be integers?
• Number of dimensions allowed
◦ Usually no real limits
◦ C: limits to one dimension, but allows array elements to be
themselves arrays
CSomputer
cience
Foundations
Array descriptors
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 22 / 29
• What information is needed by the programming
language/program about an array?
• Base address – where is the first element
• Element type
• Index type – doesn’t have to be integer for some languages
• Index lower, upper bounds
• Number of locations in array
CSomputer
cience
Foundations
Finding address of array element
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 23 / 29
• Element location (or address, A) depends on base address (B),
index (I), index lower bound (L), and element size (S)
A = B + (I − L)× S
CSomputer
cience
Foundations
Finding address of array element
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 23 / 29
• Element location (or address, A) depends on base address (B),
index (I), index lower bound (L), and element size (S)
A = B + (I − L)× S
Given an array “Taxes” whose startlocation is 1024, element type is along integer (8 bytes), and whoseindices start at 0, find location ofelement Taxes[15] 1024
1028
1032
...
Array "Taxes"
Taxes[0]
Taxes[1]
Taxes[15]
CSomputer
cience
Foundations
Finding address of array element
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 23 / 29
• Element location (or address, A) depends on base address (B),
index (I), index lower bound (L), and element size (S)
A = B + (I − L)× S
Given an array “Taxes” whose startlocation is 1024, element type is along integer (8 bytes), and whoseindices start at 0, find location ofelement Taxes[15] 1024
1028
1032
...
Array "Taxes"
Taxes[0]
Taxes[1]
Taxes[15]
• A = 1024 + (15− 0)× 8 = 1144
CSomputer
cience
Foundations
Another example
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 24 / 29
• Suppose array Foo has elements of a type that requires 6 bytes
to store, that it begins at location 4096, and that its indices begin
at 10.
• What is the address of Foo[201]?
CSomputer
cience
Foundations
Another example
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 24 / 29
• Suppose array Foo has elements of a type that requires 6 bytes
to store, that it begins at location 4096, and that its indices begin
at 10.
• What is the address of Foo[201]?
• Answer:
A = B + (I − L)× S
= 4096 + (201− 10)× 6= 5242
CSomputer
cience
Foundations
Optimizing address computation
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 25 / 29
• Can write expression so static parts can be calculated at compile
time and stored in a constant:
A = B + (I − L)× S= B + (I × S)− (L× S)= (I × S) + (B − L× S)← constant part
CSomputer
cience
Foundations
Multidimensional arrays
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 26 / 29
• Can have more than 1 dimension• E.g., 10× 10 array is 2D, 100 entries, 10 rows, 10 columns• Indices: [row,column] or [row][column]• How to store dimensions?
◦ Row-major order: Rows are kept together, elements stored
one row after anotherT: array 1..10, 1..10 of integers
row 1
...row 2 row 10
◦ Column-major order: keeps columns together
• Row-major probably more common
CSomputer
cience
Foundations
Address calculation
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 27 / 29
• Array T is an 2D array of temperatures taken over a 1-meter grid,
100 m on a side
• Assume row-major order, float data type (assume 4 bytes), base
address for T = 2048, indices each start at 0 (so 0..99)
• What is the address of T[20,15]?
CSomputer
cience
Foundations
Address calculation
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 27 / 29
• Array T is an 2D array of temperatures taken over a 1-meter grid,
100 m on a side
• Assume row-major order, float data type (assume 4 bytes), base
address for T = 2048, indices each start at 0 (so 0..99)
• What is the address of T[20,15]?
• First: what is start of row 20?
A[20,0] = B + (I − L)× S
= 2048 + (20− 0)× (4× 100) = 10, 048
CSomputer
cience
Foundations
Address calculation
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 27 / 29
• Array T is an 2D array of temperatures taken over a 1-meter grid,
100 m on a side
• Assume row-major order, float data type (assume 4 bytes), base
address for T = 2048, indices each start at 0 (so 0..99)
• What is the address of T[20,15]?
• First: what is start of row 20?
A[20,0] = B + (I − L)× S
= 2048 + (20− 0)× (4× 100) = 10, 048
• Next, find offset of element in row 20 – same kind of calculation:
A[20,15] = 10, 048 + (I − L)× S
= 10, 048 + (15− 0)× 4 = 10, 108
CSomputer
cience
Foundations
Heterogenous types: Records, structs
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 28 / 29
• Records contain heterogeneous related data
• E.g., data about employee: name (string), address (string), salary
(fixed-point integer), height (float),...
• Most languages support this type: struct in C, defstruct in
Lisp, record in Pascal, etc.
• Design issues:
◦ How are fields selected?
◦ How is field checked?
◦ Can we assign one structure to another?
◦ How to implement – e.g., how to find element from selector?
(field must have type, offset)
CSomputer
cience
Foundations
Pointers
Introduction
Variable names
Assignment
Data types
Primitive data types
• Numeric types
• Booleans
• Characters
• Arrays
• Records
• Pointers
Copyright c© 2002–2017 UMaine School of Computing and Information Science – 29 / 29
• These “point” to an object: contain its address
• Used for user-created dynamic variables (from heap)
• Used for indirect addressing (e.g., C) – abstraction of assembly
language’s indirect addressing mode
• Can assign to and through them:
int a = 3; // integer
int* p; // pointer to integer
p = &a; // p = a’s address
*p = 4; // a now = 4
• Often used (in C, e.g.) to access array elements
int a[100]; // 100-element array of ints
int* p = a; // p = addr of a’s start (no &)
for (int i=0;i<100;i++) {
*p = 0;
p++;
}