Difference between revisions of "Objective-C 2.0 Data Types"

From Techotopia
Jump to: navigation, search
(int Data Type)
Line 23: Line 23:
 
== int Data Type ==
 
== int Data Type ==
  
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 handle by the int data type is machine dependent. typically the amount of storage allocated to int values in 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.
+
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 handle by the int data type is machine dependent. Typically the amount of storage allocated to int values in 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 ints 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. 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:
 +
 
 +
<tt>int myoctal = 024;</tt>
 +
 
 +
Similarly, an int may be expressed in number base 14 (hexadecimal) by preceding the number with ''0x'', for example:
 +
 
 +
<tt>int myhex = 0xFFA2;</tt>
 +
 
 +
 
 +
 +
 
 +
== float Data Type ==
 +
 
 +
The Objective-C ''float'' data type is used to store values containing decimal places.

Revision as of 19:17, 30 September 2009

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 (where each 1 or 0 is referred to as a bit).

Humans, of course, don't think in binary. We work with 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 the following syntax:

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 the number 10 is seen by the computer in binary as:

1010

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. When converted to binary, it is stored as:

10101100011

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

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 handle by the int data type is machine dependent. Typically the amount of storage allocated to int values in 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 ints 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. 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 14 (hexadecimal) by preceding the number with 0x, for example:

int myhex = 0xFFA2;



float Data Type

The Objective-C float data type is used to store values containing decimal places.