Data TypesIn computer science, a data type (often shortened to type) is a classification system that is used to specify the possible values that a particular piece of data can take. The data type specifies how data is represented as binary 1’s and 0’s when it is stored in memory as well as how those 1’s and 0’s are interpreted when they are subsequently read back. Data types also provide a mechanism by which the operations and manipulations that can be performed on values of a particular type can be specified. For example, it makes sense to be able to perform an operation that adds two numbers together, but what about adding two pictures? How about dividing two sounds? By classifying the data in memory, data types provide a way of assigning higher-level meaning to the raw bits and bytes. This additional meaning allows us to write our apps in more abstract terms, terms that we as humans can understand. Using a relatively small set of data types along with the ability to create data types of our own we are able to express and structure almost any type of information we can imagine.
Primitive TypesFor the most part, the basic data types (also known as primitive types) that are available in Objective-C are inherited from the C programming language, the language upon which Objective-C is built. In the next section we’ll take a brief look at some of the types that are available.
Primitive Data Types in Objective-C
Integer TypesWe have already encountered the Objective-C data type int in previous posts. The int data type is short for integer and variables that have been declared as being of type int can be used to store integral or whole numbers (i.e. numbers that don’t contain a decimal point). There are two main integer types that Objective-C inherits from C:
The char Data TypeThe char data type is an 8-bit signed integer value and is commonly used to represent a single character, punctuation mark, space or symbol such as the letter ‘a’ or the ‘+’ symbol.
Escape SequencesIn addition to the characters, puntuation marks etc mentioned above, the char data type can also store a range of special characters (often referred to as escape sequences) such as the tab or newline characters. These special characters all start with a backslash ( ) followed by the special character. Commonly used special characters in Objective-C include:
- a – Sound alert
- b – Backspace
- f – Form Feed
- n – Newline
- r – Carriage Return
- t – Horizontal Tab
- v – Vertical Tab
- \ – Backslash
- “ – Double Quote (when you want to include a double quote in a string – See later in this post for more on strings)
- ‘ – Single Quote (when you want to include a singe quote in a string).
The int Data TypeThe int data type that we’ve already seen is larger than the char data type in that it uses a larger number of storage bits (a minimum of 32-bits of storage instead of just 8). As with the char data type, the int data type is signed by default and with the extra bits, can store much larger numbers than can be stored in variables of type char.
Type PrefixesNow, these two basic integer data types (char and int) aren’t the only integer data types available to you in Objective-C. You also have the option of combining them with one or more type prefixes that modify whether variables of that type will be signed or unsigned (both the basic integer types are signed by default) as well as modifying how much storage the basic data types use in memory. The valid type modifiers come in two groups. In the first group we have the sign prefixes:
- signed – indicates that signed (i.e. both positive and negative) numbers will be stored. This modifier can be used with both the char and int types.
- unsigned – indicates that only positive numbers will be stored. This modifier can also be used with both the char and int types.
- short – reduces the amount of storage used by the int type from 32-bits to 16-bits and is only valid when used in combination with the int data type.
- long – increase the amount of storage used by the int type from its default of 32-bits. This modifier (currently) only has an effect on 64-bit iOS platforms and is only valid when used in combination with the int data type.
- long long – increases the amount of storage used by the int type from 32-bits to 64-bits. It is only valid when used in combination with the int type.
unsigned char signed int unsigned short int long int unsigned long long int
Floating Point TypesFloating point numbers are numbers that have a decimal point and as we saw in my previous post on floating point numbers, values of this type are stored in memory as a combination of a sign-bit, mantissa and an exponent. There are two primitive floating point types available in Objective-C:
- The float type is a 32-bit single precision float point value that uses a single sign bit, an 8-bit mantissa and 23 bits of exponent when storing numbers. This equates closely with the definition of a single-precision data type defined by the IEEE 754 specification that we looked at in my previous post.
- The double type is used to store double-precision numbers (think IEEE 754 double-precision number) and uses twice the storage of the float data type (a minimum of 64-bits). With the double data type, the 64-bits are split into a single sign-bit, an 11-bit exponent and a 52-bit mantissa. With the additional bits, the double type can be used to store the approximately twice the range of the float data type.
float standardGravity = 9.80665; double goldenRatio = 1.61803398874; long double pi = 3.14159265358979323846;