Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Language ─ C 語言資料型態. 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework.

Similar presentations


Presentation on theme: "Introduction to C Language ─ C 語言資料型態. 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework."— Presentation transcript:

1 Introduction to C Language ─ C 語言資料型態

2 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework

3 常數與變數 常數 (constant) 指固定不變的資料,程式執行當中不能改變 其值 整數常數: 12 、 -234 、 0 、 10000 、 … 字元常數: ‘A’ 、 ‘Z’ 、 … 浮點數常數: 8.23 、 0.1232 、 0.001 、 … C++ 提供三種常數表示法 十進位整數常數:直接寫出數值。 八進位整數常數:以 0 為開頭的數字。 十六進位整數常數:以 0x 為開頭的數字。

4 常數與變數 ( 續 ) Ex. 計算十進位、八進位與十六進位的 123 、 0123 、 0x123 #include void main() { printf("%d\n",123); printf("%d\n",0123); printf("%d\n",0x123); printf("%o\n",0123); printf("%x\n",0x123); system("PAUSE"); }

5

6 常數與變數 ( 續 ) 變數 指程式中可以用來儲存資料的空間的 “ 名字 ” , 如同每個人都有一個名字一樣,當程式中需 要使用某變數時,可以用此名字來稱呼之 Ex. a=10; // 設定變數 a 的值為 10 變數在使用前需先宣告才能使用,其目的在 指定變數的資料型態 配置變數使用的記憶體空間 避免有兩個不同的變數使用相同的名字 為什麼需要宣告變數呢?

7 常數與變數 ( 續 ) 變數的命名規則 變數的命名沒有長度限制,可以包含英文字、數字 和底線符號 (_) Ex. happydog 、 happy_cat 、 class3 大小寫英文字母不能互通,也就是同一個英文字的 大小寫是不同的兩個符號 Ex. happydog 和 Happydog 是不同的變數 儘量不要以底線符號做為變數的開頭 Ex. _clearscreen 函數可以清除目前的顯示區域 變數不得與 C 語言的關鍵字名稱相同 變數名稱不能有特殊符號 ( 如: ~ 、 \ 、 @ 、 - 、 …) Ex. George&Mary 、 happy-cat

8 整數資料型態與變數宣告 整數 int 宣告方法: int v; 或是 int v=120; 整數範圍: -2147483648~2147483647 (-2 31 ~2 31 -1) 短整數 short int 宣告方法: short int v; 整數範圍: -32768~32767 (-2 15 ~2 15 -1) 長整數 long int 宣告方法: long int v; 或是 long v; 整數範圍: -2147483648~2147483647 (-2 31 ~2 31 -1)

9 整數資料型態與變數宣告 ( 續 ) 無符整數 unsigned int 宣告方法: unsigned int v; 整數範圍: 0~2 16 -1

10 整數資料型態與變數宣告 ( 續 ) Ex. 寫一程式測試短整數能否表示下列數值? 32767 、 32768 、 32769 、 -32768 、 -32769 #include void main() { short int v1=32767, v2=32768, v3=32769, v4=-32768, v5=-32769; printf("v1=%d\n",v1); printf("v2=%d\n",v2); printf("v3=%d\n",v3); printf("v4=%d\n",v4); printf("v5=%d\n",v5); system("PAUSE"); }

11 整數資料型態與變數宣告 ( 續 )

12 浮點數資料型態與變數宣告 浮點數 float 可用來宣告數學裡的實數,也就是具有小數點的數值 宣告方法: float f=123.456; 兩種表示方法 一般小數點表示法 Ex. 123.456  printf(“%f”, f); 指數表示法 Ex. 1.23456*10 2  printf(“%e”, f); 浮點數的小數位有 6 位,即便小數部份沒有那麼多位數, 仍會以 0 遞補之,如有超過位數,則會捨棄之 小數顯示位元的控制: %d.df  %8.2f

13 浮點數資料型態與變數宣告 ( 續 ) 倍精數 double 如果覺得浮點數的精密度不夠,可以將浮點 數宣告為倍精數 記憶體使用: 8 bytes 表示範圍: 1.7E+/-308 宣告方法: double pi=3.141592654;

14 浮點數資料型態與變數宣告 ( 續 ) Ex. 練習以不同的格式輸出浮點數 #include void main() { float f1=12.3456789; float f2=12.3456; printf("%f\n",f1); printf("%f\n",f2); printf("%e\n",f2); printf("%2.2f\n",f2); system("PAUSE"); }

15 浮點數資料型態與變數宣告 ( 續 )

16 字元資料型態與變數宣告 字元 char 電腦中用來顯示的字,也就是 ASCII 所指的 符號 記憶體使用: 1 byte 宣告方法: char c; 或是 char ch=‘A’;

17 字元資料型態與變數宣告 ( 續 ) Ex. 讀取使用者輸入的一個字元,接著在螢幕上 顯示該字元的 ASCII 碼及前後兩個字元 #include void main() { char c; printf("Please input a character:"); scanf("%c", &c); printf("The ascii code of %c is %d\n", c, c); printf("The character before %c is %c\n", c, c-1); printf("The character after %c is %c\n", c, c+1); system("PAUSE"); }

18 字元資料型態與變數宣告 ( 續 )

19 特殊字元表示法 電腦的字元集中有些字元是控制字元或是特 殊字元 ( 或稱為脫序字元 escape sequence) ,無 法使用鍵盤輸入或顯示字元表達  需使用 兩個字元的表示法來顯示  以 ‘ \ ’ ( 反斜線: backslash) Ex. ‘\n’  換行  ASCII 碼 10

20 字元資料型態與變數宣告 ( 續 )

21 Ex. 練習特殊字元的表示法 #include void main() { printf("The dog \has three legs.\n"); printf("\123uper man!\n"); printf("\1234uper man!\n"); printf("\x4Aump to the happy world!\n"); printf("Please save the \"APPLE\"on the directory of \'C:\\pic\'.\n"); system("PAUSE"); }

22

23 資料的大小 資料的大小 sizeof 當一個變數被宣告完畢後,編譯器就會分配 適當大小的記憶空間給此變數,如果我們想 知道這個變數佔用多少記憶體,可以使用 sizeof 敍述來求得 Ex. 試觀念下列程式,猜猜其執行結果為何?

24 #include void main() { int a; float b; double c; char d; unsigned int e; printf("The size of 'int' is %d\n", sizeof(int)); printf("The size of 'float' is %d\n", sizeof(float)); printf("The size of 'double' is %d\n", sizeof(double)); printf("The size of 'char' is %d\n", sizeof(char)); printf("The size of 'unsigned int' is %d\n", sizeof(unsigned int)); printf("The size of 'long int' is %d\n", sizeof(long int)); printf("The size of 'a' is %d\n", sizeof(a)); printf("The size of 'b' is %d\n", sizeof(b)); printf("The size of 'c' is %d\n", sizeof(c)); printf("The size of 'd' is %d\n", sizeof(d)); printf("The size of 'e' is %d\n", sizeof(e)); printf("The size of 'a*b' is %d\n", sizeof(a*b)); printf("The size of 'a*d' is %d\n", sizeof(a*d)); printf("The size of '123' is %d\n", sizeof(123)); printf("The size of \"Jack\" is %d\n", sizeof("Jack")); system("PAUSE"); }

25

26 Homework 試讓使用者輸入下述資料並將使用者輸入資料列 印於螢幕上 Please input your id: 9103001 Please input your age:18 Please input your weight: 73.6 Please input your height:178.789 Your resume: id:9103001 age:18 height:178.79 weight:73.60 使用者輸入資料 輸出結果


Download ppt "Introduction to C Language ─ C 語言資料型態. 大綱 常數與變數 整數資料型態與變數宣告 浮點數資料型態與變數宣告 字元資料型態與變數宣告 資料的大小 Homework."

Similar presentations


Ads by Google