Basic data types for the OBJECTIVE-C Foundation

Source: Internet
Author: User

Objective-c is the C language superset, so the basic type is the same, so learn C language friends, in the face of objective-c will not be too unfamiliar, after all, the data type is the same, but the data processing method is not the same, then we come together to learn the basic data type, Lay the groundwork for the whole objective-c learning


The entire data type is as shown

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/26/wKioL1SS3hCyylxmAAEvNCyA9Rc550.jpg "title=" 4bc9907b-ad72-4792-b8f0-68e8b2db6351.png "alt=" Wkiol1ss3hcyylxmaaevncya9rc550.jpg "/>

You can see that the basic data types include shaping, character, float, and enumeration, and this blog post is a major overview of these four types

1. Shaping

The integral type includes the following specific types

    • Short int

    • Int

    • Long int

    • Long Long

The most commonly used is the int, the other three kinds of we usually use less, but not without,

    • short int occupies 16 bits in memory, the value range is-2 of 15 times to 2 to 15 Times Square,

    • The int type occupies 32 bits in memory, and the value range is 2 of 31 times to 2 31 parties,

    • A long int occupies 64 bits in memory, and the value range is 2 of 63 times to 2 to 63 parties.

    • A long long and a long int are approximately 64 bits of memory, and the value range is 2 63 to 2 63 times.

Objective-c also allows the unsigned keyword to be added to the front of the integer, turning them into an unsigned integer, and with a maximum value of one more than no unsigned, such as the value range of short int is-32768-32767 Then the value range of the unsigned is 0-65535.



2. Character type

The character type represents a single character, this is nothing to say, because the learning of programming has been seen and used, the character type in objective-c only one byte, so the character type does not support Chinese (Chinese is 2 bytes)

In addition, the transfer characters we use are also part of the character category.

Commonly used are the following

\b Backspace

\ n line break

\ r return character

\ t tab

\ "Double quotation marks

\ ' Single quotation mark

\ \ Backslash

There is a difference between objective-c and C here is that the character type of objective-c is enclosed in single quotation marks, and the character type of C is enclosed in double quotation marks.


3. Floating-point type

There are three kinds of floating-point types: float,double and long double. In general, float accounts for 4 bytes, double is 8 bytes, and long double accounts for 16 bytes

Unless otherwise noted, the Objective-c compiler considers all floating-point constants to be double values. To clearly represent the float constant, you need to add an F or F to the tail of the number, for example: 12.8f



4. Enumeration type

The enumeration value is also a commonly used data structure in programming, he said that the variable contains several possible values, such as the solar system of the planet's variable values have Venus, Mercury, Jupiter, Mars, Saturn, Earth, Uranus, Neptune, like this can be defined as enumeration

Here's how to use it

Enum season {spring,summer,fall,winter};enum season Mylove=summer;

The first line represents the declaration of an enumeration type, season, whose value includes only Spring,summer,fall,winter four, the second line means, create a season value, assign it a value of summer,



5. Output format characters


In Objective-c, there is an output function called NSLog (), which represents the output string and other data types, and the "%" placeholder can be used to interpret its parameters. Use a method such as the following code:

int a = 56;

NSLog (@ "==%d==", a);

NSLog (@ "==%9d==", a); //Output integer accounted for 9 bits

NSLog (@ "==%-9d==", a); //output integer 9 bits, and left justified

NSLog (@ "==%o==", a); //output 8 binary number

NSLog (@ "==%x==", a); //Output 16 binary number


Long B = 12;

NSLog (@ "%ld", b); //output integer of type long int

NSLog (@ "%LX", b); //integer of type long int with 16 binary output


Double D1 = 2.3;

NSLog (@ "==%f==", D1); //Output floating-point number in decimal form

NSLog (@ "==%e==", D1); //output floating-point numbers in exponential form

NSLog (@ "==%g==", D1); //output floating-point numbers in the simplest form

NSLog (@ "==%9f==", D1); //output floating-point numbers in decimal form, with a minimum of 9 bits

NSLog (@ "==%9.4f==", D1); //output floating-point numbers in decimal form, at least 9 bits, 4 decimal points


Long double d2 = 2.3;

NSLog (@ "==%lf==", D1); //output long floating-point numbers in decimal form

NSLog (@ "==%le==", D1); //output long floating-point numbers in exponential form

NSLog (@ "==%lg==", D1); //output long floating-point numbers in the simplest form

NSLog (@ "==%9lf==", D1); //output long floating-point numbers in decimal form with a minimum of 9 bits

NSLog (@ "==%9.4lf==", D1); //output long floating-point numbers in decimal form, at least 9 bits, 4 decimal points


NSString *str = @ "Crazy ios";

NSLog (@ "==%@==", str); String for output objective-c


NSDate *date = [[NSDate alloc] init];

NSLog (@ "==%@==", date); //Output Objective-c object



%f%e%g Three are output placeholders for floating-point types,%f for floating-point values,%e for scientific notation, &g for using common floating-point notation or scientific notation to display floating-point values. This decision depends on the value of the exponent: if the value is less than-4 or greater than 5, the%e (scientific notation) is used, otherwise the%f (floating point count method) is used.

This article is from the "Romit Code World" blog, so be sure to keep this source http://romitlee.blog.51cto.com/6758561/1591563

Basic data types for the OBJECTIVE-C Foundation

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.