OCR H446, section 1.4.1 (a)
A data type says what kind of value a variable holds, how it is stored, how much memory it needs, and which operations make sense on it. You can add two numbers, but not two Booleans, and you can't find the square root of a name. A primitive data type is a basic type built into the language, which other structures are made from.
The primitive types
| Type | Holds | Example values | Typical storage |
|---|---|---|---|
| Integer | Whole numbers, positive, negative or zero | 7, -30, 0 |
2, 4 or 8 bytes |
| Real (floating point) | Numbers with a fractional part | 3.14, -0.5, 2.0 |
4 or 8 bytes |
| Character | A single symbol | "A", "7", "?" |
1 byte (ASCII), or more (Unicode) |
| String | A sequence of characters | "Hello", "07700 900111" |
One character each, plus the length |
| Boolean | Only true or false | True, False |
1 bit (usually 1 byte in practice) |
Integer
Used for counting and anything that can't be fractional: the number of items, ages, array indexes and loop counters. Integers are exact. The range depends on how many bits are used (see 1.4.1 (b) and (c)).
Real (floating point)
Used for measurements and calculations that can be fractional: heights, prices, averages. They are stored in floating point form (1.4.1 (g)), so many values are approximations, which can cause rounding errors. For example, 0.1 + 0.2 is not exactly 0.3 in binary.
Character
A single letter, digit, space or symbol. It is stored as a number (its character code, such as ASCII or Unicode, 1.4.1 (j)). The character "A" is stored as 65.
The character "7" is not the number 7. It is a symbol with its own code (55).
String
A sequence of characters, used for text: names, addresses, messages. Strings have operations such as finding the length, taking a substring, and joining (concatenating).
In OCR pseudocode: someText.length and someText.subString(3, 3), counting from 0.
Boolean
Only two values: true or false. They come from comparisons and are used in conditions for selection and iteration.
Choosing the right data type
| Data | Best type | Why |
|---|---|---|
| Number of students in a class | Integer | Always whole |
| A person's height in metres | Real | Has a fractional part |
| Whether a form has been submitted | Boolean | Only two possibilities |
| A postcode | String | Contains letters and digits, and is not used in calculations |
| A phone number | String | Not used in arithmetic, and a leading zero must be kept |
| A grade letter | Character | A single symbol |
| A price in pence | Integer | Exact, which avoids floating point rounding errors |
A good choice means the right amount of memory is used, validation is easier, and operations work correctly.
Casting (type conversion)
Casting converts a value from one type to another. In OCR pseudocode the functions are int, str and float:
str(3) // "3"
int("3") // 3
float("3.14") // 3.14
Without casting, "5" + "3" joins the strings to give "53", whereas 5 + 3 gives 8. Casting a value that doesn't fit, such as int("hello"), causes an error.
Worked example
Task: choose a data type for each field of a pupil record, with a reason: name, year group, test average, is a prefect, middle initial.
| Field | Type | Reason |
|---|---|---|
| Name | String | Several characters |
| Year group | Integer | A whole number |
| Test average | Real | Likely to have a fractional part |
| Is a prefect | Boolean | Yes or no |
| Middle initial | Character | A single letter |
Exam tip: name the type and give a reason that refers to the data, such as "a string, because it contains a leading zero that must be kept". Remember the difference between the character "7" and the integer 7.
Check yourself
Which data type would you use to store whether a light is on or off?
Boolean, because there are only two possible values.
Why is a phone number usually stored as a string and not an integer?
It isn't used in calculations, and an integer would lose the leading zero.
What is the difference between "7" and 7?
"7" is a character (or string) stored as a character code. 7 is an integer, stored as a number that can be used in arithmetic.
What does casting do?
It converts a value from one data type to another, for example turning the string "3" into the integer 3.