$\require{cancel}$
Variables continued. Types
- A second feature of declaring a variable is to provide the type.
- We are somewhat lucky here, we have covered a good part of this.
- A variables type or data type is the type of data it holds.
- There are several basic types in most programming languages.
- Integers
- Floating point (real, float, ...)
- Character (a value off the ascii table)
- String (a collection of 0 or more characters)
- Boolean (true, false)
- We need to provide the types so that the compiler knows how to interpret the data stored in memory.
- Integers
- Positive integers are stored in binary.
- Negative integers are stored in a form called two's compliment.
- No problems, we will deal with this later.
Floating Point
- This is the IEEE 754 format
- Think a number in scientific notation.
- Very big: $3.27\times 10^{15}$
- Very small: $9.15\times 10^{-21}$
Characters
- Sometimes this is not a type,
- If it is it is enclosed in 'x'
- But special characters are escaped with \, ie '\n' is end of line
Strings
- Usually in "" as in "Hello World"
- The " is not part of the string unless escaped
- "Say \"Hello\" to the world."
Boolean
- Used mostly for control structures
- Values of
true
or false
One collection of any data type is called an array
- This is accessed by an index
- A positive integer between 0 and the size of the array.
- All of the elements (members of the array) are the same type.
There are other data structures as well. We will discuss these later.
For now, skip 2.2.3 and 2.2.4