Objective-C语法之基本数据类型


1、新建项目

为了方便,我们新建一个Single View Application 。



输入项目名称  BaseType



Product Name: 指产品名称 ,类似于项目名称。
Company Identifier: 公司标识符,一般命名规则为 “com.公司名”
Bundle Identifier: 指包标识符,用于唯一标识应用程序,默认会根据公司标识符和产品名来组合生成
Device Family: 指该应用支持的设备类型,共三个选项:iPhone、iPad、Universal(即iPhone、iPad通用)
Include Unite Tests: 是否包含单元测试代码模板,如果勾选,Xcode会帮助生成单元测试代码模板

在项目里找到,ViewController.m 为了方便演示,在界面启动时,我们加入测试代码

2 、C语言的基本数据类型长度

  1.   
  2.        NSLog(@"The size of an int is: %lu bytes.",sizeof(int));  
  3. NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));  
  4. NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));  
  5. NSLog(@"The size of a char is: %lu bytes.",sizeof(char));  
  6. NSLog(@"The size of a float is: %lu bytes.",sizeof(float));  
  7. NSLog(@"The size of a double is: %lu bytes.",sizeof(double));  
  8. NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool));   // Do any additional setup after loading the view,  
结果:
  1. 2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.  
  2. 2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.  
  3. 2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.  
  4. 2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.  
  5. 2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.  
  6. 2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.  
  7. 2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.  
3、格式化输出数据
  1. //整型   
  2.     int integerType = 5;  
  3.     //浮点型   
  4.     float floatType = 3.1415;  
  5.     //双浮点型   
  6.     double doubleType = 2.2033;  
  7.     //短整型   
  8.     short int shortType = 200;  
  9.     //长整型   
  10.     long long int longlongType = 7758123456767L;  
  11.     //c语言字符串   
  12.     char * cstring = "this is a string!";  
  13.       
  14.       
  15.     //整型   
  16.     NSLog(@"The value of integerType = %d",integerType);  
  17.     //浮点型   
  18.     NSLog(@"The value of floatType = %.2f",floatType);  
  19.     //双浮点型   
  20.     NSLog(@"The value of doubleType = %e",doubleType);  
  21.     //短整型   
  22.     NSLog(@"The value of shortType = %hi",shortType);  
  23.     //长整型   
  24.     NSLog(@"The value of longlongType = %lli",longlongType);  
  25.     //c语言字符串   
  26.     NSLog(@"The value of cstring = %s",cstring);  

结果:

  1. 2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5  
  2. 2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14  
  3. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00  
  4. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200  
  5. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767  
  6. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!  
  • 1
  • 2
  • 下一页

相关内容