Object C data type

Source: Internet
Author: User

Objective-C data types can be divided into: basic data type, object type, and id type. Basic data types include int, float, double, and char. The object type is the pointer type declared by the class or Protocol. For example, the NSAID utoreleasepool * pool, where the NSAID oreleasepool is a class, and the NSAID oreleasepool * is its pointer type. The id type can represent any type, generally only the object type, not the basic data type. Therefore, the variable pool can also be declared as the id pool. This section focuses on the basic data types.

1.int type

Int represents an integer. Its hexadecimal representation is 0xFFED0D. In the NSLog function, the formatted string uses % I to represent a decimal integer, % o (letter o) represents an octal integer, and % # x represents a hexadecimal integer. The value range is related to the device and cannot be generalized.

2. float Type

Float indicates a single-precision floating point number. To represent a float floating point number, you can add f or F to the value, for example, 13.5f. Float floating point numbers can also be expressed by scientific notation, for example, 1.7e4. In the NSLog function, the formatted string: % f indicates the floating point number, % e indicates the scientific notation, and % g indicates the floating point number.

3. double Type

The double type represents a double-precision floating point number, which is similar to the float type. The double type occupies a byte space, which is roughly twice that of the float type. Most computers use 64-bit to represent the double type. Format the string in the NSLog function, which is the same as % f, % e, and % g of float.

4. char type

Char represents the character type. It contains a few characters and is referenced in single quotes. For example, if 'A' is used to represent special characters, use the Escape Character "\".

The data type is shown in "Code List 2-2 2.3.1-1/2. 3.1-1. m.

[Code List 2-1] 2.3.1-1/2. 3.1-1.m

 

[Cpp]
# Import <Foundation/Foundation. h>

Int main (int argc, const char * argv []) {

NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];

Int integerVar = 100;

Float floatingVar = 331.79;

Double doubleVar = 8.44e + 11;

Char charVar = 'W ';



NSLog (@ "integerVar = % I", integerVar );

NSLog (@ "floatingVar = % f", floatingVar );

NSLog (@ "doubleVar = % e", doubleVar );

NSLog (@ "doubleVar = % g", doubleVar );

NSLog (@ "charVar = % c", charVar );



[Pool drain];

Return 0;

}


The output result is as follows:

10:30:26. 921 2.3.1 [707: a0f] integerVar = 100

10:30:26. 923 2.3.1 [707: a0f] floatingVar = 331.790009

10:30:26. 924 2.3.1 [707: a0f] doubleVar = 8.4420.e + 11

10:30:26. 924 2.3.1 [707: a0f] doubleVar = 8.44e + 11

10:30:26. 924 2.3.1 [707: a0f] charVar = W

The data types in Obejctive-C can be prefixed with int, float, double, and char types. The restrictions include long, long, short, unsigned, and signed, these restrictions enhance the basic type.

· Long int, which represents a 32-bit integer in most computers and is indicated by L (or l) after an integer. For example: long int numberOfPoints = 132131100l, the format string in the NSLog function is represented by % li;

· Long int can specify a broader Integer type to ensure that the variable is at least 64-bit in width. The formatted string in the NSLog function is represented by % lli;

· Long double: You can specify a broader double type. To display this type, use L (case sensitive) at the end of the string, for example, 1.234e + 7L. The format string in the NSLog function is represented by % Lf, % Le, and % Lg;

· Short int is used to specify the storage of relatively small integers, generally half of the int type. Most computers have 16 digits;

· Unsigned int, which tells the compiler to accept only integers. Put the letter u (or U) after the value, for example, 0x00ffU. When writing an integer, you can use the letter u (or U) combined with l (or L), for example, 20000UL;

Signed char indicates that the character is related to the compiler and is generally used as a non-conforming integer.


From column 516inc

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.