讨论计算机系统内存中各类变量的内存大小

本文探讨了计算机内存(RAM)的基本概念及其与变量类型的关系。通过分析不同变量类型的内存占用情况,帮助读者理解如何选择合适的变量类型以提高程序效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        由于每一台计算机都安装了一定数量的RAM,在系统中,通常用千兆字节(GB)表示RAM的数量(例如:1GB、2GB、4GB、8GB等)。计算机中的RAM是按照顺序逐字节排列的。内存中的每一个字节都要有一个可识别的唯一地址,用于区别内存中的不同字节,从零开始增长至内存的最大值。就现在而言,我们不用了解地址的细节,C编译器会自动处理地址的问题。
        如果我们熟悉变量的类型、变量的定义、变量的赋值以及变量的运算,这样会加大我们程序的运行速度,在大型程序中,定义不同的变量在RAM运行中,会相差比较大的运算速度。例如,一个整形我们可以将他定义为int型变量,或者是double型变量,两者在RAM中占用的内存相差一倍;不仅如此,两者在计算机运行时,需要的时间相差比较大,虽然肉眼不能分别出来,但是我相信,在进行大量的数据运算时,会有分别。


/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd.
File name:
Author:Jerey_Jobs    Version:0.1    Date:
Description:
Funcion List:
*****************************************************/

#include <stdio.h>

int main()
{
    printf("\nA char                is %d bytes",sizeof(char));
    printf("\nA int                 is %d bytes",sizeof(int));
    printf("\nA shart               is %d bytes",sizeof(short));
    printf("\nA long                is %d bytes",sizeof(long));
    printf("\nA long long           is %d bytes",sizeof(long long));
    printf("\nA unsigned char       is %d bytes",sizeof(unsigned char));
    printf("\nA unsigned int        is %d bytes",sizeof(unsigned int));
    printf("\nA unsigned short      is %d bytes",sizeof(unsigned short));
    printf("\nA unsigned long       is %d bytes",sizeof(unsigned long));
    printf("\nA unsigned long long  is %d bytes",sizeof(unsigned long long));
    printf("\nA float               is %d bytes",sizeof(float));
    printf("\nA double              is %d bytes",sizeof(double));
    printf("\nA long double         is %d bytes",sizeof(long double));
    printf("\n");
    return 0;
}



[root@localhost 1012]# ./demo

A char                is 1 bytes
A int                 is 4 bytes
A shart               is 2 bytes
A long                is 4 bytes
A long long           is 8 bytes
A unsigned char       is 1 bytes
A unsigned int        is 4 bytes
A unsigned short      is 2 bytes
A unsigned long       is 4 bytes
A unsigned long long  is 8 bytes
A float               is 4 bytes
A double              is 8 bytes
A long double         is 12 bytes
[root@localhost 1012]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值