C/C++学习 - 基本类型所占字节数

来源:互联网 发布:dapaolu域名更换mp4 编辑:程序博客网 时间:2024/06/09 22:16

基本类型

这里所说的基本类型大致就是下面几种:

 int unsigned int long int long long short int char float double

对于这些类型所占字节数,其实并不是一定的。尤其是int这个类型。

在32位编译器下和64位编译器下很可能是不同的。

下面是我在64位编译器下运行结果:

各位可以在自己机子下运行查看。

////  main.cpp//  TypeBit////  Created by Alps on 15/4/1.//  Copyright (c) 2015年 chen. All rights reserved.//#include <iostream>using namespace std;int main(int argc, const char * argv[]) {    short int a = 0;    int b = 0;    unsigned int c = 0;    long int d = 0;    long long e = 0;    char f = 0;    float g = 0;    double h = 0;    printf("short int is: %lu byte\n",sizeof(a));    printf("int is : %lu byte\n",sizeof(b));    printf("unsigned int is: %lu byte\n",sizeof(c));    printf("long int is : %lu byte\n",sizeof(d));    printf("long long is : %lu byte\n",sizeof(e));    printf("char is : %lu byte\n",sizeof(f));    printf("float is : %lu byte\n",sizeof(g));    printf("double is : %lu byte\n",sizeof(h));    return 0;}

其运行结果为:

short int is: 2 byteint is : 4 byteunsigned int is: 4 bytelong int is : 8 bytelong long is : 8 bytechar is : 1 bytefloat is : 4 bytedouble is : 8 byteProgram ended with exit code: 0

所以各位记得:char是一个字节。而其他的都要看编译器和操作系统了。

对了~ 1个字节为8bit.

0 0
原创粉丝点击