Data type conversions in Object-c

Source: Internet
Author: User

Different types of data are automatically converted from left to right (from low to high) in relation to the following.

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

Table 2-3 sequence table of type conversions

Operand 1 type Operand 2 Type Converted Type
_bool, char, short int, enum type Int Int
_bool, char, short int, enum type, int Long int Long int
_bool, char, short int, enum type, int, long int Long Long Long Long
_bool, char, short int, enum type, int, long int, long long Float Float
_bool, char, short int, enum type, int, long int, long long, float Double Double
_bool, char, short int, enum type, int, long int, long long, float, double Long double Long double

If you have the following representation, where F is the float type, I is the int type, L is a long int type, S is a short int type, what is the result type?

f * i + l/s

If you follow a type conversion that is right to left, you need to force type conversions, and the coercion type conversion syntax is simple in that it is preceded by the data (the target type), but the conversion is risky and may result in loss of data that needs to be done cautiously. For example:

long int l = 6666666666;

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

int i = (int) l;

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

When a cast is embedded in another expression, it is intertwined with the precedence of the operation, and the situation becomes more complex, assuming that there are several statements:

int total = 3446;

int n = 6;

float average = total/n;

The variable that runs the float average result is 574.333, this data is more accurate than the above calculation, because (float) total Converts the total variable of type int to the total variable of float type.

This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/716454

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.