// // 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 实数,用科学计数法计