黑马程序员——C基础之sizeof运算符

------- iOS培训、android培训、java培训、期待与您交流! ----------

/*

 

 sizeof运算符是一个单目运算符

 

 作用:计算常量、变量、数据类型在内存中占用的字节数

 

 1)用sizeof计算常量在内存中占用的字节数

 sizeof(1) //整型int--> 4

 

 sizeof(2.3f) //单精度型float-->4

 

 sizeof(2.3) //双精度型double-->8

 

 sizeof('a') //字符型char-->常量 4 变量 1

 

 

 */


void test(){


    short result2 = 1;

    int result = 1;

    long result3 = 1;

    float f1 = 2.3f;

    double d1 = 2.3;

    char ch = 'a';

    result = sizeof(1); // short常量 4

    printf("result = %d\n",result);

    result = sizeof(1); // int常量 4

    printf("result = %d\n",result);

    result = sizeof(1); // long常量 4

    printf("result = %d\n",result);

    result = sizeof(2.3f); // float常量 4

    printf("result = %d\n",result);

    result = sizeof(2.3); // double常量 8

    printf("result = %d\n",result);

    //char 占用1个字节, -128~127

    //char 类型的常量存储,97int类型的数据进行存储  4个字节

    //char 类型的变量存储,把字符型的ascii码转换为二进制(1给字节)进行存储 1个字节

    result = sizeof('a'); // char常量 4

    printf("result = %d\n",result);



}


// 2sizeof计算变量在内存中占用的字节数


void test2(){


    short result2 = 1;

    int result = 1;

    long result3 = 1;

    float f1 = 2.3f;

    double d1 = 2.3;

    char ch = 'a';

    result = sizeof(result2); // short变量 2

    printf("result = %d\n",result);

    result = sizeof(result); // int变量 4

    printf("result = %d\n",result);

    result = sizeof(result3); // long变量 8

    printf("result = %d\n",result);

    result = sizeof(f1); // float变量 4

    printf("result = %d\n",result);

    result = sizeof(d1); // double变量 8

    printf("result = %d\n",result);

    //char 占用1个字节, -128~127

    //char 类型的常量存储,97int类型的数据进行存储  4个字节

    //char 类型的变量存储,把字符型的ascii码转换为二进制(1给字节)进行存储 1个字节

    result = sizeof(ch); // char变量 1

    printf("result = %d\n",result);


}



// 3) sizeof计算数据类型在内存中占用的字节数


#include <stdio.h>


int main(int argc,const char * argv[]) {

   

    short result2 = 1;

    int result = 1;

    long result3 = 1;

    float f1 = 2.3f;

    double d1 = 2.3;

    char ch = 'a';

    result = sizeof(short);// short类型 2

    printf("result = %d\n",result);

    result = sizeof(int);// int类型 4

    printf("result = %d\n",result);

    result = sizeof(long);// long类型 8

    printf("result = %d\n",result);

    result = sizeof(float);// float类型 4

    printf("result = %d\n",result);

    result = sizeof(double);// double类型 8

    printf("result = %d\n",result);

    //char 占用1个字节, -128~127

    //char 类型的常量存储,97int类型的数据进行存储  4个字节

    //char 类型的变量存储,把字符型的ascii码转换为二进制(1给字节)进行存储 1个字节

    result = sizeof(char);// char类型 1

    printf("result = %d\n",result);

    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值