01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The basic data types<br />

37<br />

What does floating point mean?<br />

In computing, floating point means that the equivalent of the decimal (or radix) point<br />

in the number can “float”: the decimal point can be placed anywhere between the<br />

significant digits that make up the number, on a number-by-number basis.<br />

By contrast, in a fixed-point number, the decimal point is always positioned with a<br />

fixed number of digits after it.<br />

<strong>Objective</strong>-C doesn’t provide any standard fixed-point data types; you can typically<br />

implement them yourself by using the existing integer data types. As an example,<br />

storing monetary values as a number of cents could be considered a fixed-point format<br />

with an implied (or fixed) decimal point positioned before the last two decimal<br />

digits. For instance, the integer value 12345 could be considered to represent the<br />

value $123.45.<br />

2.2.3 Characters and strings<br />

In addition to data types that enable the storage of numerical data are a number of<br />

other types that allow storage of other categories of data. For example, the char data<br />

type can be used as the basis for storing textual data in your application.<br />

IN THE BEGINNING THERE WAS TYPE CHAR<br />

A variable of type char can store a single character, such as the letter a, the digit 6, or<br />

a symbol such as an asterisk. Because some of these characters (such as a semicolon or<br />

curly brace) already have special meaning in <strong>Objective</strong>-C, special care must be taken<br />

when defining character constants. In general, a character constant is formed by<br />

enclosing the desired character in a pair of single quotation marks. As an example,<br />

the letter a can be assigned to a variable as shown here:<br />

char c = 'a';<br />

The char data type can be considered to be a tiny 8-bit integer, so it’s also possible to<br />

manually consult an ASCII character chart and assign a numeric value directly:<br />

char d = 97;<br />

If you refer to an ASCII character chart, you’ll notice that the value 97 represents a lowercase<br />

a, so variables c and d in the preceding examples will both store identical values.<br />

Enclosing character constants in single quotation marks helps specify most characters<br />

that are printable. But a few, such as the carriage return and newline characters,<br />

are impossible to enter into your program’s source code in this manner.<br />

<strong>Objective</strong>-C therefore recognizes several escape sequences, which allow these special<br />

characters to be placed in character constants. See table 2.4 for a list of common<br />

backslash escape sequences.<br />

By default, char is an unsigned value that can store a value between 0 and 255. Using<br />

the signed qualifier allows storage of a value between −128 and 127. In most cases, however,<br />

you should probably stick with the int data type if you want to store numbers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!