关于short变量初始为SHRT_MAX的问题

C、C++和Java语言
回复
qiu_923
帖子: 250
注册时间: 2007-05-06 10:39
送出感谢: 0
接收感谢: 0

关于short变量初始为SHRT_MAX的问题

#1

帖子 qiu_923 » 2007-07-18 12:10

我用的是64位系统
源代码

代码: 全选

#include<iostream>
#include<climits>
int main()
{
    using namespace std;
    int n_int=INT_MAX;
    short n_short=SHRT_MAX;
    long n_long=LONG_MAX;

    cout<<"int is "<<sizeof (int)<<" bytes."<<endl;
    cout<<"n_int is "<<sizeof n_int<<" bytes."<<endl;
    cout<<"INT_MAX is "<<sizeof INT_MAX<<" bytes."<<endl<<endl;

    cout<<"short is "<<sizeof (short)<<" bytes."<<endl;
    cout<<"n_short is "<<sizeof n_short<<" bytes."<<endl;
    cout<<"SHRT_MAX is "<<sizeof SHRT_MAX<<" bytes."<<endl<<endl;

    cout<<"long is "<<sizeof (long)<<" bytes."<<endl;
    cout<<"n_long is "<<sizeof n_long<<" bytes."<<endl;
    cout<<"lONG_MAX is"<<sizeof LONG_MAX<<" bytes."<<endl;
    return 0;
}
编译运行结果:

代码: 全选

int is 4 bytes.
n_int is 4 bytes.
INT_MAX is 4 bytes.

short is 2 bytes.
n_short is 2 bytes.
SHRT_MAX is 4 bytes.

long is 8 bytes.
n_long is 8 bytes.
lONG_MAX is8 bytes.
我的确把n_short初始为SHRT_MAX:

代码: 全选

 short n_short=SHRT_MAX;
为什么short类型的变量n_short的值不等于SHRT_MAX:

代码: 全选

short is 2 bytes.
n_short is 2 bytes.
SHRT_MAX is 4 bytes.
回复

回到 “C/C++/Java”