author : The dust of Wan Jing ([email protected])
Reprint Please specify source : http://blog.csdn.net/shulianghan/article/details/39135079
1. Automatic type conversion
Automatic type conversion : Basic type conversions occur when assigning a basic type variable to another basic type variable;
-- integral type-float type: There will not be much change except the type conversion to floating point type;
---Float type-- integer : Type to integral type, fractional part is discarded ;
--- integer type : The range of values becomes smaller, and overflow may occur;
Example :
-- object-c code :
/************************************************************************* > File name:09_typeautoconversion.m > Author:octopus > Mail:octopus_truth.163.com > Created Time: 9/8 11:18:53 2014 ******************** /#import <Foundation/Foundation.h> int main (int argc, Char **argv) {@autoreleasepool {/* defines an int type variable */int a = 38;/* converts the int type variable to float, the value does not change, except that the type has changed */float B = a;/* print in T-float result, print: */nslog (@ "b =%g", b);/* Defines the short type variable */short c = 38;/* assigns the short type variable to the char variable, short is automatically converted to CH AR type */char d = c;/* print short, char type, print: & */nslog (@ "D =%c", d);d ouble e = 38.3838;/* Converts the double type to int type, Fractional part auto omit */int f = e;/* print double-type int, print: */nslog (@ "F =%d", f);/* Convert double type to char type, fractional part automatically omit, if value is too large, Integer section overflow */char g = e;/* Print double-char, Print: & */nslog (@ "G =%c", g), int h = 40000;/* int type to short type, if the value is too Large, may overflow */short i = h;/* print int-&Gt Short, overflow print: -25536 */nslog (@ "i =%d", i);} }
-- compile and run :
2. Forcing type conversions
coercion type conversion : by (TypeName) You can forcibly specify the type of a variable;
examples of casts :
-- object-c code :
/************************************************************************* > File name:09_ TYPECONVERSION.M > Author:octopus > Mail:octopus_truth.163.com > Created time: 9/8 13:27:52 ************************************************************************/#import <Foundation/ Foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {int a = 38;int b = 100;/* int type is divided by int type or int type, The result is 0 */float C = a/b;/* first to float type, and then to calculate, the result is a float type */float d = (float) a/b;/* the float type to int type and then calculate, The result is a */int e = (int) 38.3838 + (int) 1.3838; NSLog (@ "c =%g, d =%g, E =%d", C, D, E);}}
--Compile Run:
3. Type auto-Boost
expression data type automatic promotion rules :
-- integral type automatic promotion : All data in the expression of the short and char types will be promoted to int type ;
-- up to the highest type : The data type of the arithmetic expression automatically increases to the highest ranked data type in the expression;
-- type level rule : from low to High : longlong --- long , int , short >float , double and long double;
code example :
-- object-c code :
/************************************************************************* > File name:09_ TYPEAUTOPROMOTE.M > Author:octopus > Mail:octopus_truth.163.com > Created time: 9/8 13:44:53 ************************************************************************/#import <Foundation/ Foundation.h>int Main (int argc, char * argv[]) {@autoreleasepool {short a = 37;/* a-2 expression, A is automatically promoted to int type */nslog (@ " Calculates the data type size of the a-2:%ld ", sizeof (A-2));/* The data type of the entire expression is converted to a double type */double b = a/2.0; NSLog (@ "b =%g", b);}}
--Compile Execution:
author : The dust of Wan Jing ([email protected])
Reprint Please specify source : http://blog.csdn.net/shulianghan/article/details/39135079
Type conversion of the "IOS development" OBJECT-C syntax