C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

      首先,sizeof操作符返回类型或变量的长度,单位是字节。注意,“字节”依赖于实现,因此在一个系统中,两字节的int可能是16位,而在另一个系统中可能是32位。
      其次,头文件climits(在老式实现中为limits.h)中包含了关于整型限制的信息。具体地说,它定义了表示各种限制的符号名称。例如,INT_MAX为int的最大取值,CHAR_BIT为字节的位数。
      
      实现代码如下:

#include "stdafx.h"
#include 
<climits>    //use limits.h for older systems
#include <iostream>
int main(int argc, char* argv[])
{
    
using namespace std;
    
int n_int = INT_MAX;    //initialize n_int to max int value
    short n_short = SHRT_MAX; //symbols defined in limits.h file
    long n_long = LONG_MAX;

    
//sizeof operator yields size of type or of variable
    cout<<"int is "<<sizeof(int)<<" bytes."<<endl;
    cout
<<"short is "<<sizeof(n_short)<<" bytes"<<endl;
    cout
<<"long is "<<sizeof(n_long)<<" bytes."<<endl<<endl;

    cout
<<"Maximum values:"<<endl;
    cout
<<"int: "<<n_int<<endl;
    cout
<<"short: "<<n_short<<endl;
    cout
<<"long: "<<n_long<<endl;

    cout
<<"Minimum int values = "<<INT_MIN<<endl;
    cout
<<"Bits per byte = "<<CHAR_BIT<<endl;
    
return 0;
}

结果如下:
posted on 2010-02-05 14:03 烟皑 阅读(688) 评论(0)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理