Dark Horse programmer _ios Development _objective-c Learning Notes _ basic data types

Source: Internet
Author: User
Tags float double

1. Data types and constants:

The most common thing we use when developing iOS programs is the basic data type, which provides 4 basic data types in objective-c: int float double and char.

1.1 variables declared as int can only be used to hold shaping values.

1.2 variables declared as float type can store floating-point type values (that is, include decimal digits):

The 1.3double type is the same as the float type, except that the former is approximately twice times more accurate than the latter.

1.4 The last is the char data type, and the char type can be used to store a single character, such as the letter A, the number 6, or a semicolon. Or declare a string with a char pointer, such as char *string = "Itheima"

In OC, any number, single character, or string, is called a constant. For example: The number 2015 is an integer constant, and the string @ "Itheima hm2015!\n" represents a constant string object. An expression that consists entirely of constants is called a constant expression.

So, an expression:

2015+4-20

is a constant expression, because each item of its expression is a constant value. But if I is declared for a variable of integer type, then the expression:

2015+4-i

is not a constant expression.

Introduction to 4 Basic data types

2.int type

In OC, an integer constant is a sequence of one or more numbers. The negative sign before the sequence surface this value is a negative number. Legal values such as: 10,-10, 2015. There is no space between numbers, for example 22 000 is an illegal constant.

There are two special formats in OC that are used to hold integer constants in a non-decimal way. First: If the first digit of the shaping value is 0, then the integer will be represented in octal notation, that is, the cardinality 8来. In this case, the value of the other remainder of the value must be a valid octal number, which must be a number between 0 and 7. For example, the value 50 in OC octal, represented by 050, is equivalent to the value 40 in decimal notation.

Similarly, the octal constant of 0177 is represented as a decimal value of 127, and the algorithm is 1*64+7*8+7 =127.

You can display integer values in octal on a terminal by using the format symbol%o in the format string that is called in NSLog ():

Cases:

1 int a =177
2 NSLog (@ "int is:%o\n", a);

In the case of the above example, the octal value of the output does not have a leading 0, and the output with 0 is as follows:

1  int a = 177;
2 NSLog (@ "% #o", a);

Second case:

If the shape constant starts with 0 and the letter x (x, either uppercase or lowercase), the value is represented by a hexadecimal (that is, the base of 16) Count Mrs. Fremont. A hexadecimal number immediately following the letter x, which can consist of any number from 0 to 9, or the letter from A to F (A to f). The letters represent numbers from 10 to 15, respectively. Therefore, to assign a hexadecimal value ffef0d to an integer constant named RGBColor, you can use the following statement:

1 rgbColor = 0xffef0d;


The format symbol%x represents a hexadecimal value that does not have a leading 0x, is a hexadecimal number in lowercase letters with A--F, requires a leading 0x, uses the format character% #x表示:

1 NSLog (@ "% #x \ n", RgbColor);

Whether it is a character, a floating point, an integer, has its corresponding value range, which is related to the amount of memory allocated to storing values of a particular type, generally does not specify the size of the amount in the language, it relies on the computer that is running, So it's called the relative amount of the device. If an integer can occupy 32 bits on a computer, you can also use 64-bit storage.

It is also important to note that Nsinteger is recommended in OC instead of int, because Nsinteger will automatically recognize the number of bits in the operating system, thus automatically converting int to 32-bit or 64-bit without having to specify it yourself, so later in iOS development, It is a standard notation to use Nsinteger instead of int.

2.float Type:

The float type can contain values that store the number of decimal digits, such as the value 3., 125.8, and-.0001 are all legal floating-point numbers. To display floating-point numbers, you can use NSLog (@ "%f", Floattype).

Floating-point constants can also be expressed in the form of scientific notation, where a value of 1.74e4 is a scientific notation, which he says is 1.7 times 10 of the 4 parties. In front of the letter e is called the Mantissa, followed by the exponent, the exponent can be placed in front of the exact and minus sign. The exponent represents 10 of the screen that will be multiplied by the mantissa. Therefore, 2.225e-3 means 2.225 times 10-3, and a value of 0.00225.

3.double type

The double type is very similar to the float type, and a double is typically used when the float does not meet the precision (i.e. the range of the provided value). A double type variable can store about twice times more bits than float, and most computers use 64 bits to represent a double value. NOTE: Unless you declare it yourself, OC will look at all floating-point constants as double values, to clearly represent the float constant, one to declare the float type, and two to add an F or F to the tail of the number, for example:

Cgsizemake (12.5f,13f);

To display values of type double, you can use%f,%e, or%g, which display the same format as the float value.

4.char type
The char type can store a single character. You can get character constants by putting them in parentheses. So, ' a '. ' 3 ' and '; ' are valid character constants. The first represents the letter A, the second represents the character 3, and the third represents a semicolon. The second one is not equal to the number 3.

character constant ' \ n ' (line break) is a valid character constant. ' \ ' is called an escape character and is used to output special symbols in NSLog. Since the backslash symbol is a special symbol in OC. The character ' \ n ' is treated as a single character in OC, although it is made up of two characters.

The following example uses the basic data type:

1 #import <Foundation/Foundation.h>

3 int main (int argc,char *argv[])

5 {

7 nsautoreleasepool *pool =[[nsautorelelasepool Alloc]init];

9 int numvar = 10;

float Floavar = 331.79;

double Doublevar =9.44e+11;

Charvar = ' W ';

NSLog (@ "numvar:=%i", Numvar);

NSLog (@ "floatvar:=%f", Floatvar);

NSLog (@ "doublevar:=%e", Doublevar);

NSLog (@ "Doublevar: =%g", Doublevar);

NSLog (@ "charvar:=%c", Charvar);

[Pool drain];

return 0;

31}

Output: Numvar = 10;

Floatvar = 331.790009;

Doublevar =8.440000e+11;

Doublevar =8.44e+11;

Charvar = ' W ';

Note: The value we assign to Floatvar is actually 331.79, but why is the output 331.790009? In fact, the values displayed are determined by a particular computer system. Use a special way to manage numbers inside your computer. Computers in the processing of numbers, it is likely to encounter inaccuracies, such as 1 divided by 3, calculated as 3.3333333, the string of 3 is actually just an approximation, theoretically, there should be an infinite number of 3. However, computers can only save these numbers, which is the result of computer uncertainty. The same uncertainties are applied here, so some floating-point values cannot be accurately represented in memory!!

Dark Horse programmer _ios Development _objective-c Learning Notes _ basic data types

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.