码迷,mamicode.com
首页 > 移动开发 > 详细

【IOS 开发】Object - C 语法 之 类型转换

时间:2014-09-09 16:03:28      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   strong   数据   

作者 : 万境绝尘 (octopus_truth@163.com)

转载请注明出处http://blog.csdn.net/shulianghan/article/details/39135079



1. 自动类型转换


自动类型转换 : 将一个基本类型变量 赋值给另外一个基本类型变量就会出现基本类型转换;

-- 整型 -> 浮点型 : 除了类型转换为浮点型之外, 不会有太大变化;

-- 浮点型 -> 整型 : 类型转为整型, 小数部分被舍弃;

-- 长整形 -> 整型 : 取值范围变小, 可能发生溢出;


示例

-- Object-C 代码

/*************************************************************************
    > 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{
		/* 定义 int 类型变量 */
		int a = 38;
		/* 将 int 类型变量转换为 float, 数值没有变化, 只是类型发生了变化 */
		float b = a;
		/* 打印int -> float 结果, 打印 : 38 */
		NSLog(@"b = %g", b);

		/* 定义 short 类型变量 */
		short c = 38;
		/* 将 short 类型变量赋值给 char 变量, short 自动转化为 char 类型 */
		char d = c;
		/* 打印 short -> char 类型, 打印 : & */
		NSLog(@"d = %c", d);

		double e = 38.3838;
		/* 将 double 类型转为 int 类型, 小数部分自动省略 */
		int f = e;
		/* 打印 double -> int 类型, 打印 : 38 */
		NSLog(@"f = %d", f);

		/* 将 double 类型转为char 类型, 小数部分自动省略, 如果数值过大, 整数部分会溢出 */
		char g = e;
		/* 打印 double -> char, 打印 : & */
		NSLog(@"g = %c", g);

		int h = 40000;
		/* 将 int 类型转为 short 类型, 如果数值过大, 可能会溢出 */
		short i = h;
		/* 打印 int -> short, 溢出 打印 : -25536 */
		NSLog(@"i = %d", i);


	}
 }

-- 编译运行

octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 09_typeAutoConversion.m 
octopus-2:oc octopus$ ./a.out 
2014-09-08 13:08:41.250 a.out[1345:507] b = 38
2014-09-08 13:08:41.252 a.out[1345:507] d = &
2014-09-08 13:08:41.252 a.out[1345:507] f = 38
2014-09-08 13:08:41.253 a.out[1345:507] g = &
2014-09-08 13:08:41.253 a.out[1345:507] i = -25536
octopus-2:oc octopus$ 



2. 强制类型转换


强制类型转换 : 通过 (typeName) 可以强行指定一个变量的类型;


强制转换示例

-- Object-C 代码

/*************************************************************************
    > File Name: 09_typeConversion.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一  9/ 8 13:27:52 2014
 ************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, char * argv[])
{
	@autoreleasepool {
		int a = 38;
		int b = 100;

		/* int 类型 与 int 类型相除 还是 int 类型, 结果是 0 */
		float c = a / b;

		/* 先将 a 转为 float 类型, 再进行计算, 得出的结果就是 float 类型 */
		float d = (float)a / b;

		/* 将 float 类型转为 int 类型后再计算, 结果是 39 */
		int e = (int)38.3838 + (int)1.3838;

		NSLog(@"c = %g, d = %g, e = %d", c, d, e );
	}
}


-- 编译运行

octopus-2:oc octopus$ ./a.out 
2014-09-08 13:31:44.361 a.out[1391:507] c = 0, d = 0.38, e = 39
octopus-2:oc octopus$ 


3. 类型自动提升


表达式数据类型自动提升规则 :

-- 整型自动提升 : 所有的表达式中得 short 和 char 类型的数据都会被提升为 int 类型

-- 提升至最高类型 : 算数表达式的数据类型自动提高到表达式中等级最高的数据类型;

-- 类型等级规则 : 从低到高 : short -> int -> long -> longlong -> float -> double -> long double;


代码示例

-- Object-C 代码

/*************************************************************************
    > File Name: 09_typeAutoPromote.m
    > Author: octopus
    > Mail: octopus_truth.163.com 
    > Created Time: 一  9/ 8 13:44:53 2014
 ************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, char * argv[])
{
	@autoreleasepool {
		short a = 37;

		/* a - 2 表达式中, a 会自动提升为 int 类型 */
		NSLog(@"计算 a - 2 的数据类型大小 : %ld", sizeof(a - 2));

		/* 整个表达式的数据类型转换为 double 类型 */
		double b = a / 2.0;
		NSLog(@"b = %g", b);
	}
}

-- 编译执行 :  

octopus-2:oc octopus$ ./a.out 
2014-09-08 13:50:27.502 a.out[1418:507] 计算 a - 2 的数据类型大小 : 4
2014-09-08 13:50:27.505 a.out[1418:507] b = 18.5
octopus-2:oc octopus$ 


作者 : 万境绝尘 (octopus_truth@163.com)

转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/39135079

【IOS 开发】Object - C 语法 之 类型转换

标签:style   blog   http   color   os   io   ar   strong   数据   

原文地址:http://blog.csdn.net/shulianghan/article/details/39135079

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!