栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言——数据类型 【32位/64位】下的字节大小

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言——数据类型 【32位/64位】下的字节大小

说明:
  本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。
  QQ 群 号:513683159 【相互学习】

一、64位机器汇编代码 源文件:datatype.c
#include 
#include 
typedef unsigned char byte;

int main(int argc,char *agv[])
{

        printf("1.char = %ldn",sizeof(char));
        printf("2.short = %ldn",sizeof(short));
        printf("3.int = %ldn",sizeof(int));
        printf("4.long = %ldn",sizeof(long));
        printf("5.float = %ldn",sizeof(float));
        printf("6.double = %ldn",sizeof(double));
        printf("7.bool = %ldn",sizeof(bool));
        printf("8.byte = %ldn",sizeof(byte));
        return 0;
}

编译运行结果:

  gcc datatype.c -g -O0 -o datatype-m64
  ./datatype

1.char = 1
2.short = 2
3.int = 4
4.long = 8
5.float = 4
6.double = 8
7.bool = 1
8.byte = 1
二、32位机器汇编代码 源文件:datatype.c
#include 
#include 
typedef unsigned char byte;

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

        printf("1.char = %dn",sizeof(char));
        printf("2.short = %dn",sizeof(short));
        printf("3.int = %dn",sizeof(int));
        printf("4.long = %dn",sizeof(long));
        printf("5.float = %dn",sizeof(float));
        printf("6.double = %dn",sizeof(double));
        printf("7.bool = %dn",sizeof(bool));
        printf("8.byte = %dn",sizeof(byte));
        return 0;
}

编译运行结果:

  gcc datatype.c -g -O0 -o datatype-m64
  ./datatype

1.char = 1
2.short = 2
3.int = 4
4.long = 4
5.float = 4
6.double = 8
7.bool = 1
8.byte = 1

三、union与struct 源文件:datatype.c
#include 
int main(int argc,char *argv[])
{
        typedef union
        {
                int a;
                long b; 
                char c; 
                int d[5];
        }UNIOn;
        int e[5];
        typedef struct
        {
                short i;
                int j;
                double k;
        }STRUCT;

        printf("%dn",sizeof(e));
        printf("%dn",sizeof(UNIOn));
        printf("%dn",sizeof(STRUCT));
        return 0;
}

编译运行结果:

64位:
  gcc datatype.c -g -O0 -o datatype-m64
  ./datatype

20
24
16

32位:
  gcc datatype.c -g -O0 -o datatype-m32
  ./datatype

20
20
16
四、sizeof(指针)的不同写法
#include 
#include 


void test_1(char str_1[100])
{
        printf("str_1:%ldn",sizeof(str_1));
}

void test_2()
{
        void *p = malloc(100);
        printf("p:%ldn",sizeof(p));

}

void test_3()
{
        char str_2[10];
        printf("str_2:%ldn",sizeof(str_2));
}

int main(int argc,char *argv[])
{
        char str[100];
        test_1(str);
        test_2();
        test_3();
        return 0;
}
#输出:(64位系统下)
str_1:8
p:8
str_2:10
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号