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

10 篇文章 5 订阅
7 篇文章 0 订阅

基本类型

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

 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 byte
int is : 4 byte
unsigned int is: 4 byte
long int is : 8 byte
long long is : 8 byte
char is : 1 byte
float is : 4 byte
double is : 8 byte
Program ended with exit code: 0

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

对了~ 1个字节为8bit.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值