Objective-C 變數和基本的資料類型

來源:互聯網
上載者:User

標籤:

OC是增強了C的特性,所以在變數和基礎資料型別 (Elementary Data Type)上基本與C一致。

在OC中變數命名有如下規則:

由字母、數字、底線、$符號組成

必須以字母、底線、$符號開頭

大小寫敏感

在OC中定義變數的時候不能使用OC的保留字,OC的保留字如下:

 

 

 

OC中有如下基礎資料型別 (Elementary Data Type):

int:聲明整型變數

 

double:聲明雙精確度變數

 

float:聲明浮點型變數

 

char:聲明字元型變數

 

id:通用的指標類型

 

enum:聲明枚舉類型

 

long:聲明長整型變數或函數

 

short:聲明短整型變數或函數

 

signed:聲明有符號類型變數

 

struct:聲明結構體變數

 

union:聲明共用體(聯合)資料類型

 

unsigned:聲明無符號類型變數

 

void:聲明函數無傳回值或無參

 

資料類型格式化字元:

資料類型 oc關鍵字 格式說明引導符

 

整型 int %d.%i

 

短整型 short int %hd.%hi

 

長類型 long int %ld.%li

 

無符號短整型 unsigned int %u

 

無短整型 unsigned short %hu

 

無符號長整型 unsigned long %lu

 

浮點型 float %f

 

雙精確度型 double %f

 

長雙精確度型 long double %lf

 

字元型 char %c

 

下面有一個例子程式:

 

[plain] view plain copy

 

  1. //  
  2. //  main.m  
  3. //  mxy01-dataType  
  4. //  
  5. //  Created by mxy on 13-9-7.  
  6. //  Copyright (c) 2013年 mxy. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. int main(int argc, const char * argv[])  
  12. {  
  13.   
  14.     @autoreleasepool {  
  15.           
  16.         //保留字是不能定義為變數名的,例如下面的int、float等就不能作為變數名  
  17.         int i = 2;  
  18.         float f = 2.3f;  
  19.         double d = 2.3e12;  
  20.         char c = ‘a‘;  
  21.           
  22.         //輸出資料  
  23.         NSLog(@"i : %d",i);  
  24.         NSLog(@"f : %f 截取後 : %.2f",f,f);  
  25.         NSLog(@"d : %e 截取後 : %.2e",d,d);  
  26.         NSLog(@"c : %c , %d",c ,c );  
  27.           
  28.         //資料轉換  
  29.         //資料類型容量大的轉成小的可能會丟失精度  
  30.         int i2 = (int)f;  
  31.         float f2 = (float)i ;  
  32.           
  33.         NSLog(@"資料轉換");  
  34.         NSLog(@"i2 : %d",i2);  
  35.         NSLog(@"f2 : %f",f2);  
  36.           
  37.         NSLog(@"變數的範圍");  
  38.         if(YES){  
  39.             int i3 = 2;  
  40.              NSLog(@"i3 : %d",i3);  
  41.         }  
  42.         //在if的{}外面是無法訪問到內部的i3變數的,因為i3變數的範圍就只是那對{}內部  
  43.         //NSLog(@"i3 : %d",i3);  
  44.         /*  
  45.          運行結果  
  46.          2013-09-07 22:47:52.655 mxy01-dataType[859:303] i : 2  
  47.          2013-09-07 22:47:52.667 mxy01-dataType[859:303] f : 2.300000 截取後 : 2.30  
  48.          2013-09-07 22:47:52.672 mxy01-dataType[859:303] d : 2.300000e+12 截取後 : 2.30e+12  
  49.          2013-09-07 22:47:52.674 mxy01-dataType[859:303] c : a , 97  
  50.          2013-09-07 22:47:52.679 mxy01-dataType[859:303] 資料轉換  
  51.          2013-09-07 22:47:52.682 mxy01-dataType[859:303] i2 : 2  
  52.          2013-09-07 22:47:52.685 mxy01-dataType[859:303] f2 : 2.000000  
  53.          */  
  54.     }  
  55.     return 0;  
  56. }  

 

Objective-C 變數和基本的資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.