Data type of OBJECTIVE-C

Source: Internet
Author: User
Tags integer

OBJECTIVE-C data types can be divided into: basic data types, object types, and ID types. The basic data types are: int, float, double, and char types. The object type is the type of pointer declared by a class or protocol, such as: NSAutoreleasePool * pool, where NSAutoreleasePool is a class, and NSAutoreleasePool * is its pointer type. The ID type can represent any type, typically just the object type, not the base data type, so the variable pool can also be declared as an ID pool. In this section, we will focus on basic data types.

1.int type

The int type represents an integer, its hexadecimal representation: 0xffed0d, using the%i to represent a decimal integer in a format string using the NSLog function,%o (letter O) Represents a 8-binary integer,% #x表示十六进制整数. Its range of values is related to the device and cannot be generalized.

2. Float type

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

3. Double type

The double type represents a double-precision floating-point number, similar to the float type, in which the double type occupies roughly twice times the float type. Most computers represent the double type with 64 digits. The NSLog function formats the string in the same%f,%e, and%g as float.

4. Char type

The char type represents a character type, holds a few characters, and is quoted in single quotes. For example: ' A ', if you want to represent some special characters, use the escape character "\".

The data type instance is shown in "Code listing 2-2 2.3.1-1/2.3.1-1.m".

"Code Listing 2-1" 2.3.1-1/2.3.1-1.M

#import <Foundation/Foundation.h> 
     
int main (int argc, const char * argv[]) { 
     
    NSAutoreleasePool * pool = [NS Autoreleasepool alloc] init]; 
     
         int integervar = m; 
     
         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 data type in Obejctive-c can be preceded by a qualifier in int, float, double, and char types, with qualifiers such as long, long, short, unsigned, and signed, which enhance the base type.

· A long int, representing a 32-bit integer on most computers, is represented by an L (or L) after the integer, for example: the formatted string in a long int numberofpoints = 131071100l,nslog function is represented by a%li;

· A long int can specify a wider integer type to ensure that the variable is at least 64 bits wide. The format string in the NSLog function is expressed using%lli;

· Long double, you can specify a wider double type, showing that this can be used at the tail end using L (case), 1.234e+7l. The format string in the NSLog function is expressed using%LF,%le, and%LG;

· The short int specifies that a relatively small integer is stored, typically half the type of int. Most computers are 16 bits;

· unsigned int, tells the compiler to just accept integers, and then put the letter u (or u) after the value, for example: 0x00ffu; When writing integers, you can combine the letter u (or U) and L (or L), for example: 20000UL;

Signed Char, the character represented by the compiler, and is generally used as a non conforming integer.

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.