Object-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;
}


转载于:https://my.oschina.net/zhangqs008/blog/200154

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值