156
技術社區[雲棲]
Objective-C中的基本數據類型
//
// main.m
// 01.基本數據類型
//
// Created by zhangqs008 on 14-2-13.
// Copyright (c) 2014年 zhangqs008. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//01.基本數據類型長度
NSLog(@"01.基本數據類型長度");
NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool));
//02.格式化輸出
NSLog(@"02.格式化輸出");
int integerType = 5;//整型
float floatType = 3.1415; //浮點型
double doubleType = 2.2033;//雙浮點型
short int shortType = 200;//短整型
long long int longlongType = 7758123456767L;//長整型
char * cstring = "this is a string!";//c語言字符串
//整型
NSLog(@"The value of integerType = %d",integerType);
//浮點型
NSLog(@"The value of floatType = %.2f",floatType);
//雙浮點型
NSLog(@"The value of doubleType = %e",doubleType);
//短整型
NSLog(@"The value of shortType = %hi",shortType);
//長整型
NSLog(@"The value of longlongType = %lli",longlongType);
//c語言字符串
NSLog(@"The value of cstring = %s",cstring);
//03.NSString與NSInteger的相互轉換
NSLog(@"03.NSString與NSInteger的相互轉換");
NSInteger integerNumber = 888;
NSString *string = [NSString stringWithFormat:@"%ld",(long)integerNumber];
NSLog(@"string is %@", string);
NSInteger integer = [string intValue];
NSLog(@"integer is %ld", (long)integer);
//04.int,NSInteger,NSUInteger,NSNumber
//1.當需要使用int類型的變量的時候,可以像寫C的程序一樣,用int,也可以用NSInteger,但更推薦使用NSInteger,因為這樣就不用考慮設備是32位的還是64位的。
//2.NSUInteger是無符號的,即沒有負數,NSInteger是有符號的。
//3.NSInteger是基礎類型,但是NSNumber是一個類。如果想要在NSMutableArray裏存儲一個數值,直接用NSInteger是不行的,必須轉換為數字對象;
}
return 0;
}
輸出結果:
2014-02-13 21:19:33.633 01.基本數據類型[1463:303] 01.基本數據類型長度
2014-02-13 21:19:33.634 01.基本數據類型[1463:303] The size of an int is: 4 bytes.
2014-02-13 21:19:33.635 01.基本數據類型[1463:303] The size of a short int is: 2 bytes.
2014-02-13 21:19:33.635 01.基本數據類型[1463:303] The size of a long int is: 8 bytes.
2014-02-13 21:19:33.635 01.基本數據類型[1463:303] The size of a char is: 1 bytes.
2014-02-13 21:19:33.636 01.基本數據類型[1463:303] The size of a float is: 4 bytes.
2014-02-13 21:19:33.636 01.基本數據類型[1463:303] The size of a double is: 8 bytes.
2014-02-13 21:19:33.636 01.基本數據類型[1463:303] The size of a bool is: 1 bytes.
2014-02-13 21:19:33.637 01.基本數據類型[1463:303] 02.格式化輸出
2014-02-13 21:19:33.637 01.基本數據類型[1463:303] The value of integerType = 5
2014-02-13 21:19:33.637 01.基本數據類型[1463:303] The value of floatType = 3.14
2014-02-13 21:19:33.638 01.基本數據類型[1463:303] The value of doubleType = 2.203300e+00
2014-02-13 21:19:33.639 01.基本數據類型[1463:303] The value of shortType = 200
2014-02-13 21:19:33.639 01.基本數據類型[1463:303] The value of longlongType = 7758123456767
2014-02-13 21:19:33.640 01.基本數據類型[1463:303] The value of cstring = this is a string!
2014-02-13 21:19:33.640 01.基本數據類型[1463:303] 03.NSString與NSInteger的相互轉換
2014-02-13 21:19:33.640 01.基本數據類型[1463:303] string is 888
2014-02-13 21:19:33.641 01.基本數據類型[1463:303] integer is 888
Program ended with exit code: 0
附:格式化輸出符號:
%@ 對象
%d, %i 整數
%u 無符整形
%f 浮點/雙字
%x, %X 二進製整數
%o 八進製整數
%zu size_t
%p 指針
%e 浮點/雙字 (科學計算)
%g 浮點/雙字
%s C 字符串
%.*s Pascal字符串
%c 字符
%C unichar
%lld 64位長整數(long long)
%llu 無符64位長整數
%Lf 64位雙字
%e 實數,用科學計數法計
最後更新:2017-04-03 12:55:06