课时35整型基础
C的10种整型数据类型
sizeof(char)=1
sizeof(unsigned char)=1
sizeof(short)=2
sizeof(unsigned short)=2
sizeof(int)=4
sizeof(unsigned int)=4
sizeof(long)=4
sizeof(unsigned long)=4
sizeof(long long)=8
sizeof(unsigned long long)=8
请按任意键继续. . .
#include <stdio.h> #include <stdlib.h> int main() { printf("sizeof(char)=%zd ", sizeof(char)); printf("sizeof(unsigned char)=%zd ", sizeof(unsigned char)); printf("sizeof(short)=%zd ", sizeof(short)); printf("sizeof(unsigned short)=%zd ", sizeof(unsigned short)); printf("sizeof(int)=%zd ", sizeof(int)); printf("sizeof(unsigned int)=%zd ", sizeof(unsigned int)); printf("sizeof(long)=%zd ", sizeof(long)); printf("sizeof(unsigned long)=%zd ", sizeof(unsigned long)); printf("sizeof(long long)=%zd ", sizeof(long long)); printf("sizeof(unsigned long long)=%zd ", sizeof(unsigned long long)); system("pause"); return 0; }
课时40整型数据的存储(字节序)
int data = 0x12345678;
#include <stdio.h> #include <stdlib.h> int main() { int data = 0x12345678; char *p = (char *)&data; printf("%p %hhx ", &(p[0]), p[0]); printf("%p %hhx ", &(p[1]), p[1]); printf("%p %hhx ", &(p[2]), p[2]); printf("%p %hhx ", &(p[3]), p[3]); system("pause"); return 0; }
输出
00000034B476FBE4 78
00000034B476FBE5 56
00000034B476FBE6 34
00000034B476FBE7 12
请按任意键继续. . .
78是低字节,12是高字节。
这是小端字节序
字节序分为:小端字节序,大端字节序、网络字节序。
小端字节序:低字节存于内存低地址;高字节存于内存高地址。
大端字节序:高字节存于内存低地址;低字节存于内存高地址。
网络字节序作为中介,是大端字节序。
课时41整型转换、扩展与截断
扩展:
char src;
int dst;
dst = (char)src;
src有符号,扩展位=符号位,符号位0扩展位也是0,符号位1扩展位也是1
src无符号,扩展位=0
截断:
int src;
char dst;
dst =(char)src;
从低字节开始,截取sizeof(dst)个字节的十六进制数值
整型转换,只与十六进制数值有关系
int i = -1;
整数-1
0xFFFFFFFF十六进制数值
扩展:
int to unsigned int
#include <stdio.h> #include <stdlib.h> int main() { int int_num = -1; unsigned int uint_num = (unsigned int)int_num;//强制转换,显式转换 printf("%x %d ", int_num, int_num); printf("%x %u ", uint_num, uint_num); system("pause"); return 0; }
ffffffff -1
ffffffff 4294967295
请按任意键继续. . .
char to int
char to unsigned int
#include <stdio.h> #include <stdlib.h> int main() { char char_num = -1; int int_num = (int)char_num; unsigned int uint_num = (unsigned int)char_num; printf("%hhx %hhd ", char_num, char_num); printf("%x %d ", int_num, int_num); printf("%x %u ", uint_num, uint_num); system("pause"); return 0; }
ff -1
ffffffff -1
ffffffff 4294967295
请按任意键继续. . .
char to int
char to unsigned int
#include <stdio.h> #include <stdlib.h> int main() { char char_num = 1; int int_num = (int)char_num; unsigned int uint_num = (unsigned int)char_num; printf("%hhx %hhd ", char_num, char_num); printf("%x %d ", int_num, int_num); printf("%x %u ", uint_num, uint_num); system("pause"); return 0; }
1 1
1 1
1 1
请按任意键继续. . .
unsigned char to int
unsigned char to unsigned int
#include <stdio.h> #include <stdlib.h> int main() { unsigned char char_num = 0xFF; int int_num = (int)char_num; unsigned int uint_num = (unsigned int)char_num; printf("%hhx %hhd ", char_num, char_num); printf("%x %d ", int_num, int_num); printf("%x %u ", uint_num, uint_num); system("pause"); return 0; }
ff -1
ff 255
ff 255
请按任意键继续. . .
截断:
#include <stdio.h> #include <stdlib.h> int main() { int int_num = -0x12345678; short short_num = (short)int_num; unsigned short unshort_num = (unsigned short)int_num; printf("int %x %d ", int_num, int_num); printf("short %hx %hd ", short_num, short_num); printf("unshort %hx %hu ", unshort_num, unshort_num); system("pause"); return 0; }
int edcba988 -305419896
short a988 -22136
unshort a988 43400
请按任意键继续. . .
课时42整型常量
sizeof(0xFFLL), sizeof(0xFFLLu)//long long, unsigned long long
sizeof(0xFFL), sizeof(0xFFLu)//long, unsigned long
#include <stdio.h> #include <stdlib.h> int main() { printf("sizeof %u %u ", sizeof(0xFFLL), sizeof(0xFFLLu));//long long, unsigned long long printf("sizeof %u %u ", sizeof(0xFFL), sizeof(0xFFLu));//long, unsigned long system("pause"); return 0; }
sizeof 8 8
sizeof 4 4
请按任意键继续. . .