当前位置:网站首页>C语言各数据类型的内存映像
C语言各数据类型的内存映像
2022-06-11 16:07:00 【李肖遥】
关注、星标公众号,直达精彩内容
来源:网路素材
C语言各种数据类型的内存映像(32位平台):

0、signed char
#include <stdio.h>int main(){ char min = 1<<7; char max = (1<<7)-1; for(int i=min;i<=max;i++) if(i<0) printf("%.2X ",(unsigned char)i); else
{ printf("%c ",i); if(i%32==0) printf("\n%d ",i);
}
getchar();
}output:

1、整型的signed和unsigned
#include <stdio.h>int main(){ signed int smin = 1<<31; signed int smax = (1<<31)-1; printf("%d\n",smin); // -2147483648
printf("%d\n",smax); // 2147483647
unsigned int umax = -1; printf("%u\n",umax); // 4294967295
umax = (1<<32)-1; printf("%u\n",umax); // 4294967295}如果一个表达式同时包含signed和unsigned整型,signed会提升为unsgined,可能会隐藏一些意想不到的错误,特别是用在比较运算时:
unsigned int a=4294967290; int b=-6;
printf("%d\n",a==b); // 1 , b promotes to unsigned2、double的二进制位显示
#include <stdio.h>void printByte(double d){ int bs = sizeof d; unsigned char *ch = (unsigned char*)&d; for(int i=0;i<bs;i++) printf("%.2X ",*(ch+i));
}int main(){ int n = 0x01020304; if(*(char*)&n == 4) printf("小端:");//小端:
double d = 15.75; // 1111.11, 指数位值:1023+3
//0 100 0000 0010 1111100000000000000000000000000000000000000000000000
printByte(d);//00 00 00 00 00 80 2F 40
// 40 2F 80
// 0100 0000 0010 1111 1000 0000
getchar();
}将double分成4部分显示:
#include <stdio.h>typedef struct packed_double {
unsigned int low32; // 小数位 低32位
unsigned int low20:20; // 小数位 低33-52位
unsigned int exp11:11; // 指数位 低53-63位,移码1023+二进制整数位-1
unsigned int sign:1; // 符号位} packed_double;typedef union { double d;
packed_double b;
} packed;int main(){
packed pd;
pd.d = -15.75;
pd.d = 12.3; printf("%u %u %u %u\n",pd.b.sign,pd.b.exp11,pd.b.low20,pd.b.low32);
getchar();
return 0;
}/*
0 1026 1015808 0
*/3、数组是相同数据类型的依次存储
数组名是一个存储数据首元素地址具有常量性质的特殊指针,成员是相对于基址的偏移:
#include <stdio.h>void printArr(short arr[],int len){ for(int i=0;i<len;i++)
{ printf("%d ",*(arr+i));
} printf("\n");
}int main(){
short arr[] = {1,3,2}; int len = sizeof arr / sizeof *arr;
printArr(arr,len);
}4、枚举只是枚举可以取值的一些符号常量的一个特殊整型
#include <stdio.h>int main(){ enum Nm{LOSS,TIE,WIN}nm; // 实质是一个整型,成员只是可能的右值(符号常量)的枚举
nm = LOSS; printf("%d ",nm); // 0
nm = TIE; printf("%d ",nm); // 1
nm = WIN; printf("%d ",nm); // 2
nm = (enum Nm)3;
printf("%d ",nm); // 3
printf("\n%d",sizeof(enum Nm)); // 4
getchar();
}枚举让相关符号常量内聚为一组,相对于#define,枚举对数据的描述性更清晰。
5、共用体成员的起始地址相同,共用一块内存空间,值相互覆盖
#include <stdio.h>int main(){ union Nn{int a; double b;}nn;// 成员的起始地址相同,值相互覆盖
nn.a = 123; //
printf("起始地址:%X,内存空间占用:%d\n",&nn.a,sizeof nn.a);
nn.b = 12.3; printf("起始地址:%X,内存空间占用:%d\n",&nn.a,sizeof nn.b);
nn.a = 12; printf("起始地址:%X,内存空间占用:%d\n",&nn.a,sizeof nn.a);
getchar();
}/*
起始地址:12FF40,内存空间占用:4
起始地址:12FF40,内存空间占用:8
起始地址:12FF40,内存空间占用:4
*/当一些事物具有更多共性,但有少量差异时,可以只用一个内嵌一个共用体的结构体来描述:
#include <stdio.h>#include <string.h>#define MAXPARTS 12struct Parts{ // 零件
int cost; char supplier[12]; char unit[12] ;
};struct Assembly{ // 装配件
int n_parts; struct {
char partno[12];
short quan;
}parts[MAXPARTS];
};struct Inventory{ // 存货类型,或是零件,或是装配件
char partno[10]; int quan; enum{PART,ASSEMBLY}type; // 存货类型
union { struct Parts parts;
struct Assembly assembly;
}info;
};int main(){ struct Inventory screen;
strcpy(screen.partno,"p001");
screen.quan = 12;
screen.type = Inventory::PART;
screen.info.parts.cost = 122; strcpy(screen.info.parts.supplier,"hw"); strcpy(screen.info.parts.unit,"pcs");
struct Inventory shell;
strcpy(shell.partno,"a001");
shell.quan = 4;
shell.type = Inventory::ASSEMBLY;
shell.info.assembly.n_parts=22; strcpy(shell.info.assembly.parts[0].partno,"d001");
shell.info.assembly.parts[1].quan = 5; int costs; if(shell.type == Inventory::ASSEMBLY)
costs = shell.info.assembly.n_parts;
printf("%d\n",costs); //22
getchar(); return 0;
}6、结构体是不同数据类型的数据依次存储在一起
结构体各数据成员的引用可以通过其内存大小和字节对齐来相对于基址偏移来计算。结构体通常用于描述某一事物,用其成员来描述该事物的某些关键属性。让该事物既可以用结构体变量整体表示,也可以对其成员分别引用来处理该事物的各个属性。
#include <stdio.h>int main()
{ struct demo{char a; short b;int c;} abc; // 成员相对于基址偏移,字节对齐
abc.b=12; short *p = (short*)((int)&abc+sizeof(short)); // 模拟编译器计算第2个成员的偏移地址
printf("%d %d\n",abc.b,*p); // 12 12
printf("%d\n",sizeof(struct demo));// 8
getchar();
}7、位域是对整型数据的按位处理
(一次可以处理n个位,1<=n<=整形长度)
位域(全局)二进制位显示:
#include <stdio.h>void printBinM(unsigned int n){ for(int i=31;i>=0;i--) printf("%d",(n & 1<<i)>>i); printf("\n");
}struct Bf{
unsigned a:3;
unsigned b:4;
unsigned c:5;
}bf;int main(){
bf.a =1;
bf.b=15;
bf.c=3; int *p = (int*)&bf; // 505
printf("%d\n",*p);
printBinM(*p);//00000000000000000000000111111001
getchar();
}位域(局部)二进制位显示:
#include <stdio.h>void printBinM(unsigned int n){ for(int i=31;i>=0;i--) printf("%d",(n & 1<<i)>>i); printf("\n");
}int main(){ struct Bf{
unsigned a:3;
unsigned b:4;
unsigned c:5;
}bf;
bf.a =1;
bf.b=15;
bf.c=3; int *p = (int*)&bf; // -858996231
printf("%d\n",*p);
printBinM(*p);//11001100110011001100000111111001
getchar();
}版权声明:本文来源网络,免费传达知识,版权归原作者所有。如涉及作品版权问题,请联系我进行删除。
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
关注我的微信公众号,回复“加群”按规则加入技术交流群。点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看。边栏推荐
- DHCP protocol instantiation analysis
- Detailed explanation of MySQL binlog log and master-slave replication
- 3000 words to teach you how to use mot
- 瑞吉外卖项目(三)员工管理业务开发
- Question ac: Horse Vaulting in Chinese chess
- Database design recommendations
- leetcode-141. Circular linked list
- 什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?
- Using cloud DB to build apps quick start - quick games
- Code farming essential SQL tuning (Part 1)
猜你喜欢

What is a generic? Why use generics? How do I use generics? What about packaging?

How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test

laravel 2020-01-01T00:00:00.000000Z 日期转化

Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library

Will you be punished for not wearing seat belts in the back row?

Learn how to parse SQL from kernel code

Pyqt5 enables the qplaintextedit control to support line number display

MySQL快速入门实例篇(入内不亏)

What if you can't access the desktop after the computer is turned on

Interview high frequency algorithm question --- longest palindrome substring
随机推荐
leetcode785. 判断二分图(中等)
Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]
Using cloud DB to build app quick start -server
WGet command use
Golang map basic operations
基于FPGA的VGA协议实现
DHCP协议实例化分析
问题 AC: 中国象棋中的跳马问题
Customized thread communication (lock) of JUC
Application of AI in index recommendation
[从零开始学习FPGA编程-17]:快速入门篇 - 操作步骤2-5- VerilogHDL硬件描述语言符号系统与程序框架(软件程序员和硬件工程师都能看懂)
[sword finger offer] 22 The penultimate node in the linked list
最近7天,最近10天,最近90天时间处理逻辑
Time processing logic for the last 7 days, the last 10 days, and the last 90 days
Elk enterprise log analysis system
Db4ai: database driven AI
laravel8 实现签到功能案例
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
PostgreSQL source code compilation
Code farming essential SQL tuning (Part 1)
