第四章 4.9 sizeof运算符

4.28编写一段程序,输出每一种内置类型所占空间大小。

#include <iostream>
#include <string>
using namespace std;

int main(){
    int a;
    short b;
    long c;
    char d;
    wchar_t e;
    bool f;
    float g;
    double h;
    long double i;
    unsigned int j;
    unsigned short k;
    unsigned long l;
    cout << "int:" << sizeof a << endl;
    cout << "short:" << sizeof b << endl;
    cout << "long:" << sizeof c << endl;
    cout << "char:" << sizeof d << endl;
    cout << "wchar_t:" << sizeof e << endl;
    cout << "bool:" << sizeof f << endl;
    cout << "float:" << sizeof g << endl;
    cout << "double:" << sizeof h << endl;
    cout << "long double:" << sizeof i << endl;
    cout << "unsigned int:" << sizeof j << endl;
    cout << "unsigned short:" << sizeof k << endl;
    cout << "unsigned long:" << sizeof l << endl;
    return 0;
}

结果:

int:4
short:2
long:4
char:1
wchar_t:2
bool:1
float:4
double:8
long double:8
unsigned int:4
unsigned short:2
unsigned long:4

4.29

int x[10]; int *p = x;
cout << sizeof(x) / sizeof(*x) << endl;//10
cout << sizeof(p) / sizeof(*p) << endl;//1

4.30

(a)sizeof x + y -----sizeof(x + y)
(b)sizeof p->mem[i]-----sizeof(p->mem[i])
(a)sizeof a < b -----sizeof(a < b)
(b)sizeof f()-----sizeof(f())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值