1 / 17

Chapter 2 基本資料型態

Chapter 2 基本資料型態. 簡介. 整數 字元 浮點數 ( 小數 ) 初始值宣告和常數 型態的捨進位 型態轉換. 整數. int, unsigned int 4 bytes long, unsigned long 4 bytes int, long –2,147,483,648 to 2,147,483,647 unsigned int, unsigned long 0 to 4,294,967,295 short, unsigned short 2 bytes short - 32,768 to 32,767

ardith
Download Presentation

Chapter 2 基本資料型態

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2基本資料型態

  2. 簡介 • 整數 • 字元 • 浮點數(小數) • 初始值宣告和常數 • 型態的捨進位 • 型態轉換

  3. 整數 • int, unsigned int 4 bytes • long, unsigned long 4 bytes • int, long –2,147,483,648 to 2,147,483,647 • unsignedint, unsignedlong 0 to 4,294,967,295 • short, unsigned short 2 bytes • short -32,768 to 32,767 • unsigned short 0 to 65,535

  4. 字元 • 其實就是整數 • 字元內碼取決您的作業系統 • char, signed char 1 byte -128 to 127 • unsigned char 1 byte 0 to 255

  5. 數值極限 • 不同的型別有不同的極限值 • Minimum, maximum, 之類的 • 整數極限定義在 • <limits.h> • 浮點數極限定義在 • <float.h>

  6. 整數極限範例 #include <stdio.h> #include <stdlib.h> #include <limits.h> int main(int argc, char *argv[]) { printf("char:[%d, %d]\n", CHAR_MIN, CHAR_MAX); printf("short:[%d, %d]\n", SHRT_MIN, SHRT_MAX); printf("int:[%d, %d]\n", INT_MIN, INT_MAX); printf("long:[%ld, %ld]\n", LONG_MIN, LONG_MAX); system("pause"); return 0; }/* char: [-128, 127]short: [-32768, 32767]int: [-2147483648, 2147483647]long: [-2147483648, 2147483647] */

  7. 浮點數 • float 4 byte -3.402823466E+38 to -1.175494351E-38 ,0 , 1.175494351E-38 to 3.402823466E+38 • double, long double 8 byte -1.7976931348623157E+308 to -2.2250738585072014E-308 ,0, 2.2250738585072014E-308 to 1.7976931348623157E+308

  8. 浮點數例子 #include <stdio.h> #include <stdlib.h> #include <float.h> int main(int argc, char *argv[]) { printf("float:[%g, %g]\n", FLT_MIN, FLT_MAX); printf("double:[%g, %g]\n", DBL_MIN, DBL_MAX); printf("long double:[%Lg, %Lg]\n", LDBL_MIN, LDBL_MAX); system("pause"); return 0; }/* float:[1.17549e-038, 3.40282e+038] double:[2.22507e-308, 1.79769e+308] long double:[2.22507e-308, 1.79769e+308] */

  9. 浮點數精度不足的例子 #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(int argc, char *argv[]) { float x = ULONG_MAX; /* 4,294,967,295 */ double y = ULONG_MAX; long double z = ULONG_MAX; printf("%f\n%f\n%Lf\n", x, y, z); system("pause"); return 0; }/* 4294967296.000000 //是由較大的型別轉換為較小的型別,可能導致資料遺失 4294967295.000000 4294967295.000000 */

  10. 初始值宣告 • int i = 9, j = 017, k = 0x7f; • char c = ‘a’, c2 = 97; • long n = 1234567L; • float x = 1.0F; • double y = 2.3; • long double z = 4.5L; • char string[] = “hello”;

  11. 特殊字元常數 • ‘\n’換行 • '\t' tab • '\0' null byte (ASCII 0) • '\\' (\) • ‘\b’ 退位 • ‘\ddd’八進位組合

  12. 常數 • 一個不能被修改的變數 • 需使用const修飾字 • 宣告時需指定值:const int a = 7; • 不能用再多維陣列 • 但是C++可以

  13. 巨集替換指令 • 需使用 #define來定義 • 用來預先定義常數(編譯前) • 另一種定義常數的方法 • C++已經取消 • 在C中 可以用來決定陣列維度#define SIZE 100int a[SIZE];/* "int a[100];" */

  14. 型態的捨進位 • 常發生在 浮點數 和 整數相互轉換 • 整數運算會無條件捨去小數int i = 2; int j = 3; int k = i/j;/* k == 0 !!! */ • 浮點數運算則常發生捨入錯誤 • 因為結果無法直接塞入目標型態

  15. 進位 和 轉換 • 所有整數運算都會使用 int 或 long • 數字的目標型態取決於其值 • 較小的運算目標型態會適時的加寬 • 需注意型態寬度有可能會縮減 • 如果值不是在更小的型態的範圍, 結果是不確定

  16. 強制型態轉換 • 一種使用者自行指明的型態轉換 • 使用括號來指定目標型別int i = (int)x; • 另一個強迫轉成float的例子float x = (float)i / j;

  17. 總結 基本資料型態 • C的內建資料型態都是以數值儲存 • 整數或是浮點數 • 整數運算會無條件捨去分數 • 浮點數運算的值是不明確的 • 運算後的資料型別會被適時的加寬 • 可以使用強制型態轉換來明確定義目標型別

More Related