int unsigned int 4 bytes long unsigned long
整數 • 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 – unsigned short 0 to 65, 535 3
字元 • 其實就是整數 – 字元內碼取決您的作業系統 • char, signed char 1 byte -128 to 127 • unsigned char 1 byte 0 to 255 4
整數極限範例 #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] */ 6
浮點數 • float 4 byte -3. 402823466 E+38 to -1. 175494351 E-38 , 0 , 1. 175494351 E-38 to 3. 402823466 E+38 • double, long double 8 byte -1. 7976931348623157 E+308 to -2. 2250738585072014 E-308 , 0, 2. 2250738585072014 E-308 to 1. 7976931348623157 E+308 7
浮點數例子 #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. 17549 e-038, 3. 40282 e+038] double: [2. 22507 e-308, 1. 79769 e+308] long double: [2. 22507 e-308, 1. 79769 e+308] */ 8
浮點數精度不足的例子 #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("%fn%Lfn", x, y, z); system("pause"); return 0; }/* 4294967296. 000000 //是由較大的型別轉換為較小的型別,可能導致資料遺失 4294967295. 000000 */ 9
初始值宣告 • int i = 9, j = 017, k = 0 x 7 f; • char c = ‘a’, c 2 = 97; • long n = 1234567 L; • float x = 1. 0 F; • double y = 2. 3; • long double z = 4. 5 L; • char string[] = “hello”; 10
- Slides: 17