龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

求32位机器上unsigned int的最大值及int的最大值的解决方法

时间:2014-05-19 04:26来源:网络整理 作者:网络 点击:
分享到:
本篇文章是对求32位机器上unsigned int的最大值及int的最大值的解决方法进行了详细的分析介绍,需要的朋友参考下
代码如下:

#include <stdio.h>
int main(int argc, char *argv[])
{
 unsigned int max_int = 0-1;
 printf("The max value of unsigned int on 32 machine: %u/n", max_int);
}

代码如下:

#include <stdio.h>
int main(int argc, char *argv[])
{
 unsigned int max_int = 0-1;
 printf("The max value of unsigned int on 32 machine: %u/n", max_int);
}

gcc编译后:
int_sizeof1.c: 在函数‘main'中:
int_sizeof1.c:5: 警告:整数溢出
运行后:
The max value of int on 32 machine: 4294967295
 
VC6.0和java编译后,无错误。
运行后:
The max value of int on 32 machine: 4294967295
代码如下:

#include <stdio.h>
int main(int argc, char *argv[])
{
 int max_int = (1<<31)-1;
 printf("The max value of int on 32 machine: %d/n", max_int);
}

将其int写成有符号型的程序如下:
代码如下:

#include <stdio.h>
int main(int argc, char *argv[])
{
 int max_int = (1<<31)-1;
 printf("The max value of int on 32 machine: %d/n", max_int);
}

gcc编译后:
int_sizeof1.c: 在函数‘main'中:
int_sizeof1.c:5: 警告:整数溢出
运行后:
The max value of int on 32 machine: 2147483647
VC6.0和java编译后,无错误。
运行后:
The max value of int on 32 machine: 2147483647
因为int的最高位是符号位。
精彩图集

赞助商链接