Changes

Jump to: navigation, search

Objective-C 2.0 Data Types

3,380 bytes added, 15:26, 5 October 2009
double Data Type
== 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 floatrather 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 any object, regardless of its type. == 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. === long === 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: <pre>long int mylargeint;</pre> 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: <pre>long double mydouble;</pre> === long long === It is safe to think of the '''long long'' qualifier as being equivalent to ''extra long''. In the case of a ''int'' data type, the application of a ''long long'' qualifier typically will change the range from 32-bit up to 64-bit: <pre>long long int mylargeint;</pre> === short === 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 value range to −32,768 to +32,767: <pre>short int myshort;</pre> === 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 an 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: <pre>unsigned int myint;</pre> Qualifiers may also be combined, for example to declare an unsigned, short integer: <pre>unsigned short int myint = 10;</pre> Note that what using ''unsigned'', ''signed',' ''short'' and ''long'' with integer values, the ''int'' keyword is optional. The following are all valid: <pre>short myint;long myint;unsigned myint;signed myint;</pre>

Navigation menu