zoukankan      html  css  js  c++  java
  • 转:C/C++基本数据类型所占字节数

    参考:http://blog.csdn.net/vast_sea/article/details/8076934

    关于这个基本的问题,很早以前就很清楚了,C标准中并没有具体给出规定那个基本类型应该是多少字节数,而且这个也与机器、OS、编译器有关,比如同样是在32bits的操作系统系,VC++的编译器下int类型为占4个字节;而tuborC下则是2个字节。

    所以int,long int,short int的宽度都可能随编译器而异。但有几条铁定的原则(ANSI/ISO制订的):

    • sizeof(short int)<=sizeof(int)

    • sizeof(int)<=sizeof(long int)

    • short int至少应为16位(2字节)

    • long int至少应为32位。

    下面给出不同位数编译器下的基本数据类型所占的字节数:



    16位编译器


    char :1个字节
    char*(即指针变量): 2个字节
    short int : 2个字节
    int:  2个字节
    unsigned int : 2个字节
    float:  4个字节
    double:   8个字节
    long:   4个字节
    long long:  8个字节
    unsigned long:  4个字节


    32位编译器


    char :1个字节
    char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节。同理64位编译器)
    short int : 2个字节
    int:  4个字节
    unsigned int : 4个字节
    float:  4个字节
    double:   8个字节
    long:   4个字节
    long long:  8个字节
    unsigned long:  4个字节


    64位编译器

    char :1个字节
    char*(即指针变量): 8个字节
    short int : 2个字节
    int:  4个字节
    unsigned int : 4个字节
    float:  4个字节
    double:   8个字节
    long:   8个字节
    long long:  8个字节
    unsigned long:  8个字节

    以下参考:http://blog.csdn.net/vast_sea/article/details/8076897

    机器字长:是指计算机进行一次整数运算所能处理的二进制数据的位数(整数运算即定点整数运算)。机器字长也就是运算器进行定点数运算的字长,通常也是CPU内部数据通路的宽度。现在一般为32位即4个字节,也有64位和16位的。

         算术类型的存储空间按照机器而定。一般,short类型为半个机器字长,int为一个机器字长,long为1或2个机器字长,float为一个机器字长,double为两个字,long double用3或4个字长。C++标准规定的是每个算术类型的最小存储空间,但其并不阻止编译器用更大的存储空间。如果要保证移植性,尽量用__int16 __int32 __int64吧,或者自己typedef int INT32一下。

    数据类型名称 字节数 别名 取值范围
    int * signed,signed int 操作系统决定,即与操作系统的"字长"有关
    unsigned int * unsigned 由操作系统决定,即与操作系统的"字长"有关
    __int8 1 char,signed char –128 到 127
    __int16 2 short,short int,signed short int –32,768 到 32,767
    __int32 4 signed,signed int –2,147,483,648 到 2,147,483,647
    __int64 8 –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
    bool 1 false 或 true
    char 1 signed char –128 到 127
    unsigned char 1 0 到 255
    short 2 short int,signed short int –32,768 到 32,767
    unsigned short 2 unsigned short int 0 到 65,535
    long 4 long int,signed long int –2,147,483,648 到 2,147,483,647
    long long 8 none (but equivalent to __int64) –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
    unsigned long 4 unsigned long int 0 到 4,294,967,295
    enum * 由操作系统决定,即与操作系统的"字长"有关
    float 4 3.4E +/- 38 (7 digits)
    double 8 1.7E +/- 308 (15 digits)
    long double 8 1.7E +/- 308 (15 digits)
    wchar_t 2 __wchar_t 0 到 65,535

    (P:指针的大小为定值4个字节)

  • 相关阅读:
    2020年. NET Core面试题
    java Context namespace element 'component-scan' and its parser class ComponentScanBeanDefinitionParser are only available on JDK 1.5 and higher 解决方法
    vue 淡入淡出组件
    java http的get、post、post json参数的方法
    vue 父子组件通讯案例
    Vue 生产环境解决跨域问题
    npm run ERR! code ELIFECYCLE
    Android Studio 生成apk 出现 :error_prone_annotations.jar (com.google.errorprone:error) 错误
    记忆解析者芜青【总集】
    LwIP应用开发笔记之十:LwIP带操作系统基本移植
  • 原文地址:https://www.cnblogs.com/kira2will/p/3987423.html
Copyright © 2011-2022 走看看