標籤:style blog http color os io ar strong 資料
作者 : 萬境絕塵 ([email protected])
轉載請註明出處 : 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 = 382014-09-08 13:08:41.252 a.out[1345:507] d = &2014-09-08 13:08:41.252 a.out[1345:507] f = 382014-09-08 13:08:41.253 a.out[1345:507] g = &2014-09-08 13:08:41.253 a.out[1345:507] i = -25536octopus-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 = 39octopus-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 的資料類型大小 : 42014-09-08 13:50:27.505 a.out[1418:507] b = 18.5octopus-2:oc octopus$
作者 : 萬境絕塵 ([email protected])
轉載請註明出處 : http://blog.csdn.net/shulianghan/article/details/39135079
【IOS 開發】Object - C 文法 之 類型轉換