求32位机器上unsigned int的最大值及int的最大值的解决方法
求32位机器上unsigned int的最大值及int的最大值的解决方法
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:#includeintmain(intargc,char*argv[]){unsignedintmax_int=0-1;p...

复制代码 代码如下:

#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的最高位是符号位。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类