Objective-C 2.0 Data Types

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Building and Installing GNUstep on LinuxWorking with Variables and Constants in Objective-C

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

When we look at the different types of software that run on computer systems, from financial applications to graphics intensive games, it is easy to forget that computers are really just binary machines. Binary systems work in terms of 0 and 1, true or false, set and unset. All the data sitting in RAM, stored on disk drives and flowing through circuit boards and buses are nothing more than sequences of 1s and 0s. Each 1 or 0 is referred to as a bit and bits are grouped together in blocks of 8, each group being referred to as a byte. When people talk about 32-bit and 64-bit computer systems they are talking about the number of bits that can be handled simultaneously by the CPU bus. A 64-bit CPU, for example, is able to handle data in 64-bit blocks, resulting in faster performance than a 32-bit based system.

Humans, of course, don't think in binary. We work with decimal numbers, letters and words. In order for a human to easily (easily being a relative term in this context) program a computer, some middle ground between human and computer thinking is needed. This is where programming languages such as Objective-C come into play. Programming languages allow us humans to express instructions to a computer in terms and structures we understand, and then compile it down to a format that can be executed by a computer.

One of the fundamentals of any program involves data, and programming languages such as Objective-C define a set of data types that allow us to work with data in a format we understand when writing a computer program. For example, if we want to store a number in an Objective-C program we could do so with syntax similar to the following:

int mynumber = 10;

In the above example, we have created a variable named mynumber of data type integer by using the keyword int. We then assigned the value of 10 to this variable. Once we know that int means we are specifying a variable of integer data type we have an understanding of what is happening in this particular line of an Objective-C program. When we compile the source code down to the machine code used by the CPU, the number 10 is seen by the computer in binary as:


Similarly, we can express a letter, the visual representation of a digit ('0' through to '9') or punctuation mark (referred to in computer terminology as characters) using the following syntax:

char myletter = 'c';

Once again, this is understandable by a human programmer, but gets compiled down to a binary sequence for the CPU to understand. In this case, the letter 'c' is represented by the decimal number 99 using the ASCII table (an internationally recognized standard that assigns numeric values to human readable characters). When converted to binary, it is stored as:


Now that we have a basic understanding of the concept of data types and why they are necessary we can take a closer look at the different data types and qualifiers supported by Objective-C.


int Data Type

<google>IOSBOX</google> The Objective-C int data type can store a positive or negative whole number (in other words a number with no decimal places). The actual size or range of integer that can be handled by the int data type is machine and compiler implementation dependent. Typically the amount of storage allocated to int values is either 32-bit or 64-bit depending on the implementation of Objective-C on that platform or the CPU on which the compiler is running. It is important to note, however, that the operating system also plays a role in whether int values are 32 or 64-bit. For example the CPU in a computer may be 64-bit but the operating system running on it may only be 32-bit.

For example, on a 32-bit implementation, the maximum range of an unsigned int is 0 to 4294967295. On a 64-bit system this range would be 0 to 18,446,744,073,709,551,615. When dealing with signed int values, the ranges are −2,147,483,648 to +2,147,483,647 and −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 for 32-bit and 64-bit implementations respectively.

When writing an Objective-C program, the only guarantee you have is that an int will be at least 32-bits wide. To avoid future problems when compiling the code on other platforms it is safer to limit int values to the 32-bit range, rather than assume that 64-bit will be available.

By default, int values are decimal (i.e. based on number base 10). To express an int in Octal (number base 8) simply precede the number with a zero (0). For example:

int myoctal = 024;

Similarly, an int may be expressed in number base 16 (hexadecimal) by preceding the number with 0x, for example:

int myhex = 0xFFA2;

char Data Type

The Objective-C char data type is used to store a single character such as a letter, numerical digit or punctuation mark or space character. For example, the following lines assign a variety of different characters to char type variables:

char myChar = 'w';
char myChar = '2';
char myChar = ':';

Special Characters/Escape Sequences

In addition to the standard set of characters outlined above, there is also a range of special characters (also referred to as escape sequences) available for specifying items such as a new line or tab. These special characters are identified by prefixing the character with a backslash (a concept referred to as escaping). For example, the following assigns a new line to the variable named newline:

char newline = '\n';

In essence, any character that is preceded by a backslash is considered to be a special character and is treating accordingly. This raises the question as to what to do if you actually want a backslash character. This is achieved by escaping the backslash itself:

char myslash = '\\'; Assign a backslash to a variable

Commonly used special characters supported by Objective-C are as follows:

\a - Sound alert
\b - Backspace
\f - Form feed
\n - New line
\r - Carriage return
\t - Horizontal tab
\v - Vertical tab
\\ - Backslash
\" - Double quote (used when placing a double quote into a string declaration)
\' - Single quote (used when placing a double quote into a string declaration)

float Data Type

The Objective-C float data type is used to store floating point values, in other words values containing decimal places. For example, 456.12 would be stored in a float data type. In practice, all floating point values are stored as a different data type (called double) by default. We will be covering the double data type next, but if you specifically want to use a float data type, you must append an f into the end of the value. For example:

float myfloat = 123.432f

For convenience when working with exceptionally large number, both floating point and double data type values can be specified using scientific notation (also known as standard form or exponential notation). For example, we can express 67.7 x 104 in Objective-C as:

float myfloat = 67.7e4

double Data Type

The Objective-C double data type is used to store larger values than can be handled by the float data type. The term double comes from the fact that a double can handle values twice the size of a float. As previously mentioned, all floating point values are stored as double data types unless the value is followed by an 'f' to specifically specify a float rather than as a double.

id Data Type

As we will see in later chapters of this book, Objective-C is an object oriented language. As such much of the way a program will be structured is in the form of reusable objects. These objects are called upon to perform tasks and return results. Often, the information passed into an object and the results returned will be in the form of yet another object. The id data type is a general purpose data type that can be used to store a reference to any object, regardless of its type.

BOOL Data Type

Objective-C, like other languages, includes a data type for the purpose of handling true or false (1 or 0) conditions. Such a data type is declared using either the _Bool or BOOL keywords. Both of the following expressions are valid:

_Bool flag = 0;
BOOL secondflag = 1;

Objective-C Data Type Qualifiers

So far we have looked at the basic data types offered within the context of the Objective-C programming language. We have seen that data types are provided for a number of different data declaration and storage needs and that each data type has associated with it some constraints in terms of what kind of data it can hold. In fact, it is possible to modify some of these constraints by using qualifiers. A number of such qualifiers are available and we will look at each one in turn in the remainder of this chapter.


The long qualifier is used to extend the value range of a data type. For example, to increase the range of an integer variable, the declaration is prefixed by the qualifier:

long int mylargeint;

The amount by which a data type's range is increased through the use of the long qualifier is system dependent, though on many modern systems int and long int both have the same range, making use of the qualifier unnecessary. The long qualifier may also be applied to the double data type. For example:

long double mydouble;

long long

It is safe to think of the long long qualifier as being equivalent to extra long. In the case of an int data type, the application of a long long qualifier typically will change the range from 32-bit up to 64-bit:

long long int mylargeint;


So far we have looked at qualifiers that increase the storage space, and thereby the value range, of data types. The short qualifier can be used to reduce the storage space and range of the int data type. This effectively reduces the integer to 16-bits in width, limiting the signed value range to −32,768 to +32,767:

short int myshort;

signed / unsigned

By default, an integer is assumed to be signed. In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction. For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually −2,147,483,648 to +2,147,483,647. If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295. An unsigned int is specified as follows:

unsigned int myint;

Qualifiers may also be combined, for example to declare an unsigned, short integer:

unsigned short int myint = 10;

Note that what using unsigned, signed, short and long with integer values, the int keyword is optional. The following are all valid:

short myint;
long myint;
unsigned myint;
signed myint;


Data types are the basic building blocks of just about every programming language and Objective-C is no exception. Now that we have covered these basics we will move on to the next chapter and begin talking about the use of variables.

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

PreviousTable of ContentsNext
Building and Installing GNUstep on LinuxWorking with Variables and Constants in Objective-C