首页 > 代码库 > 网易云课堂_艾叔:零基础一站式C语言|C程序设计精讲_章节5整型_课时41整型转换、扩展与截断
网易云课堂_艾叔:零基础一站式C语言|C程序设计精讲_章节5整型_课时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\n", int_num, int_num); printf("%x %u\n", 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\n", char_num, char_num); printf("%x %d\n", int_num, int_num); printf("%x %u\n", 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\n", char_num, char_num); printf("%x %d\n", int_num, int_num); printf("%x %u\n", 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\n", char_num, char_num); printf("%x %d\n", int_num, int_num); printf("%x %u\n", 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\n", int_num, int_num); printf("short %hx %hd\n", short_num, short_num); printf("unshort %hx %hu\n", unshort_num, unshort_num); system("pause"); return 0; }
int edcba988 -305419896
short a988 -22136
unshort a988 43400
请按任意键继续. . .
网易云课堂_艾叔:零基础一站式C语言|C程序设计精讲_章节5整型_课时41整型转换、扩展与截断
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。