Objective-C初探【3】之Data Types and Expressions

OC和别的语言一样,有很多的内置数据类型。如int,float等等。

下面我们要学习下基本的数据类型,并描述一些基本的规则以及在Objective-C中一些算术表达式。

1.int       就是整型,比较简单,不做详细介绍

2.float    单精度浮点型,可表示小数,

3.double     双精度浮点型,同样表示小数(但是可以精度是float的两倍)

4.char     表示字符,用'aa','12'的表示形式

 

就上诉4个类型而言,可以通过%格式化成不同的显示形式

类型  NSLog显示形式
int %i、%x、%o
float %f、%e、%g、%a
double %f、%e、%g、%a
char %c 
 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);

 

 

 

算数表达式

Objective-c中,毫无疑问的也有加减乘除分别表示为+、-、*、/。

以下是一个简单的OC运算代码实例

 1 #import <Foundation/Foundation.h>
 2 int main (int argc, char * argv[])
 3 {
 4   @autoreleasepool {
 5   int a = 25;
 6   int b = 2;
 7   float c = 25.0;
 8   float d = 2.0;
 9   NSLog (@"6 + a / 5 * b = %i", 6 + a / 5 * b);
10   NSLog (@"a / b * b = %i", a / b * b);
11   NSLog (@"c / d * d = %f", c / d * d);
12   NSLog (@"-a = %i", -a);
13 }
14 return 0;
15 }

输出的内容如下:

6 + a / 5 * b = 16
a / b * b = 24
c / d * d = 25.000000
-a = -25

 

 

 

这个章节的内容比较简单,但是既然学习,还是需要循序渐进的。。

posted @ 2015-10-20 14:44  HealthTree  阅读(149)  评论(0编辑  收藏  举报