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

本文介绍了Objective-C的基本数据类型,包括整型、浮点型等,并提供了格式化输出示例。同时,还展示了简单的算术运算代码实例。

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

 

 

 

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

转载于:https://www.cnblogs.com/treekang/p/4894412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值