1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <stdio.h> #include <stdint.h> #include <limits.h> #include <float.h>
void demonstrate_data_types(void) { printf("=== C语言基本数据类型 ===\n"); printf("整型数据类型:\n"); printf("char: %zu字节, 范围: %d ~ %d\n", sizeof(char), CHAR_MIN, CHAR_MAX); printf("short: %zu字节, 范围: %d ~ %d\n", sizeof(short), SHRT_MIN, SHRT_MAX); printf("int: %zu字节, 范围: %d ~ %d\n", sizeof(int), INT_MIN, INT_MAX); printf("long: %zu字节, 范围: %ld ~ %ld\n", sizeof(long), LONG_MIN, LONG_MAX); printf("long long: %zu字节\n", sizeof(long long)); printf("\n无符号整型:\n"); printf("unsigned char: %zu字节, 范围: 0 ~ %u\n", sizeof(unsigned char), UCHAR_MAX); printf("unsigned short: %zu字节, 范围: 0 ~ %u\n", sizeof(unsigned short), USHRT_MAX); printf("unsigned int: %zu字节, 范围: 0 ~ %u\n", sizeof(unsigned int), UINT_MAX); printf("unsigned long: %zu字节, 范围: 0 ~ %lu\n", sizeof(unsigned long), ULONG_MAX); printf("\n浮点型:\n"); printf("float: %zu字节, 精度: %d位, 范围: %e ~ %e\n", sizeof(float), FLT_DIG, FLT_MIN, FLT_MAX); printf("double: %zu字节, 精度: %d位, 范围: %e ~ %e\n", sizeof(double), DBL_DIG, DBL_MIN, DBL_MAX); printf("long double: %zu字节\n", sizeof(long double)); printf("\n指针类型:\n"); printf("void*: %zu字节\n", sizeof(void*)); printf("int*: %zu字节\n", sizeof(int*)); printf("char*: %zu字节\n", sizeof(char*)); printf("\n布尔型:\n"); printf("_Bool: %zu字节\n", sizeof(_Bool)); printf("\n固定宽度整型:\n"); printf("int8_t: %zu字节\n", sizeof(int8_t)); printf("int16_t: %zu字节\n", sizeof(int16_t)); printf("int32_t: %zu字节\n", sizeof(int32_t)); printf("int64_t: %zu字节\n", sizeof(int64_t)); }
void demonstrate_type_conversion(void) { printf("\n=== 数据类型转换 ===\n"); int i = 10; float f = i; double d = f; printf("隐式转换:\n"); printf("int %d -> float %.2f -> double %.2f\n", i, f, d); double pi = 3.14159; int truncated = (int)pi; printf("\n显式转换:\n"); printf("double %.5f -> int %d (截断)\n", pi, truncated); char c1 = 100; char c2 = 50; int result = c1 + c2; printf("\n整型提升:\n"); printf("char %d + char %d = int %d\n", c1, c2, result); int signed_val = -1; unsigned int unsigned_val = signed_val; printf("\n有符号与无符号转换:\n"); printf("signed int %d -> unsigned int %u\n", signed_val, unsigned_val); unsigned char max_char = 255; unsigned char overflow = max_char + 1; printf("\n溢出示例:\n"); printf("unsigned char 255 + 1 = %u (溢出)\n", overflow); }
|