ObjectC data type conversion

Source: Internet
Author: User

Automatic type conversion or forced type conversion can be performed based on the storage space occupied by data types. The general principle is that the data type of small storage capacity can be automatically converted to the Data Type of large storage capacity.

Different types of data are automatically converted from left to right (from low to high) according to the following relationship,

_ Bool, char, short int, enumeration type-> int-> long-> float-> double-> long double.

If these data types are mixed, data of different types in the operation is first converted to the same type, and then operated. The conversion is automatically converted from left to right, as shown in Table 2-3.

Table 2-3 type conversion sequence table

Operand 1 Type Operand 2 type Converted type
_ Bool, char, short int, and enumeration type Int Int
_ Bool, char, short int, enumeration type, int Long int Long int
_ Bool, char, short int, enumeration type, int, long int Long Long
_ Bool, char, short int, enumeration type, int, long int, long Float Float
_ Bool, char, short int, enumeration type, int, long int, long, float Double Double
_ Bool, char, short int, enumeration type, int, long int, long, float, double Long double Long double
 

If there is the following expression, where f is of the float type, I is of the int type, l is of the long int type, and s is of the short int type, what type is the result?

F * I + l/s

The running result is of the float type, because f is another float operand and float operation. The result is of the float type.

If the type conversion is followed by the right-to-left condition, the forced type conversion is required. The syntax of the forced type conversion is simple, that is, to add (the target type) before the data ), however, such conversions are risky and may cause data loss. Therefore, proceed with caution. For example:

Long int l = 6666666666;

NSLog (@ "l = % li", l );

Int I = (int) l;

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

The result of running is that the 6666666666 value has exceeded the capacity of the int type, so data loss occurs.

L = 6666666666

I =-1923267926

Forced conversion is sometimes embedded in other expressions. It is intertwined with the running priority, and the situation becomes more complex. Suppose there are the following statements:

Int total = 3446;

Int n = 6;

Float average = total/n;

The result of running the float variable average is 574, And the decimal point content is truncated. If we use the following statement:

Int total = 3446;

Int n = 6;

Float average = (float) total/n;

The average of the variable whose float is completed is 574.333, which is more accurate than the above calculation. This is because (float) total first converts the total variable of the int type to the total variable of the float type.


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.