强制转换也是不行的!
示例代码是:
#include <stdio.h>
int main(int argc,char **argv)
{
int a = 100 ;
short b = (short)a ;
printf("%d\n",b);
return 0;
}
以上本身在short数据类型就没有溢出,而当a 赋值本身就很大时一样会报错。
如:
#include <stdio.h>
int main(int argc,char **argv)
{
int a = 10000000000000 ;
short b = (short)a ;
printf("%d\n",b);
return 0;
}
运行结果
index.cpp: In function 'int main(int, char**)':
index.cpp:6:13: warning: overflow in implicit constant conversion [-Woverflow]
int a = 10000000000000 ;
^~~~~~~~~~~~~~
-24576