当前位置:网站首页>Memory image of various data types in C language
Memory image of various data types in C language
2022-06-11 16:21:00 【Lixiaoyao】
Focus on 、 Official account of star standard , Straight to the highlights
source : Network material
C Language memory images of various data types (32 Bit platform ):

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、 The whole type signed and 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}If an expression contains both signed and unsigned integer ,signed Will be promoted to unsgined, Some unexpected errors may be hidden , Especially when used in comparison operations :
unsigned int a=4294967290; int b=-6;
printf("%d\n",a==b); // 1 , b promotes to unsigned2、double Binary display of
#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(" The small end :");// The small end :
double d = 15.75; // 1111.11, Exponential bit value :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();
}take double Divide into 4 Partial display :
#include <stdio.h>typedef struct packed_double {
unsigned int low32; // Decimal places low 32 position
unsigned int low20:20; // Decimal places low 33-52 position
unsigned int exp11:11; // Exponential position low 53-63 position , Shift the code 1023+ Binary integer digits -1
unsigned int sign:1; // Sign bit } 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、 An array is a sequential storage of the same data type
The array name is a special pointer that stores the address of the first element of data and has the property of constant , Members are offset from the base address :
#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、 Enumeration is just a special integer type that enumerates some symbolic constants that can take values
#include <stdio.h>int main(){ enum Nm{LOSS,TIE,WIN}nm; // In essence, it is an integer , Members are just possible right values ( Symbolic constant ) Enumeration
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();
}Enumeration causes related symbolic constants to be condensed into a group , be relative to #define, Enumerations are more descriptive of data .
5、 The starting address of the community member is the same , Share a memory space , Values overlap each other
#include <stdio.h>int main(){ union Nn{int a; double b;}nn;// The starting address of the member is the same , Values overlap each other
nn.a = 123; //
printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.a);
nn.b = 12.3; printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.b);
nn.a = 12; printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.a);
getchar();
}/*
Initial address :12FF40, Memory footprint :4
Initial address :12FF40, Memory footprint :8
Initial address :12FF40, Memory footprint :4
*/When something has more in common , But with a few differences , It can be described only by a structure with a common body embedded in it :
#include <stdio.h>#include <string.h>#define MAXPARTS 12struct Parts{ // Spare parts
int cost; char supplier[12]; char unit[12] ;
};struct Assembly{ // Assembly parts
int n_parts; struct {
char partno[12];
short quan;
}parts[MAXPARTS];
};struct Inventory{ // Inventory type , Or parts , Or assembly parts
char partno[10]; int quan; enum{PART,ASSEMBLY}type; // Inventory 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、 A structure is a collection of data of different data types
The reference of each data member of the structure can be calculated by its memory size and byte alignment relative to the base address offset . A structure is usually used to describe something , Use its members to describe some key attributes of the thing . Let this thing be expressed as a whole with structural variables , You can also refer to its members separately to handle the attributes of the thing .
#include <stdio.h>int main()
{ struct demo{char a; short b;int c;} abc; // Member offset from base , Byte alignment
abc.b=12; short *p = (short*)((int)&abc+sizeof(short)); // The simulation compiler evaluates the 2 The offset addresses of members
printf("%d %d\n",abc.b,*p); // 12 12
printf("%d\n",sizeof(struct demo));// 8
getchar();
}7、 Bit fields are bitwise processing of integer data
( It can be dealt with at one time n bits ,1<=n<= Shaping length )
Bit field ( overall situation ) Binary bit display :
#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();
}Bit field ( Local ) Binary bit display :
#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();
}Copyright notice : Source network of this paper , Free delivery of knowledge , The copyright belongs to the original author . If involves the work copyright question , Please contact me to delete .
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
Pay attention to my WeChat official account , reply “ Add group ” Join the technical exchange group according to the rules . Click on “ Read the original ” See more sharing , Welcome to share 、 Collection 、 give the thumbs-up 、 Looking at .边栏推荐
- postgresql源码编译
- 09 Minimum Spanning Tree highway
- TC8:UDP_ MessageFormat_ 01-02
- Connect to the database using GSQL
- Opengauss AI capability upgrade to create a new AI native database
- DHCP protocol instantiation analysis
- Project workspace creation steps - Zezhong ar automated test tool
- Classmate, have you heard of mot?
- 09-最小生成树 公路村村通
- C starts an external EXE file and passes in parameters
猜你喜欢

用户界面之工具栏详解-AutoRunner自动化测试工具
![Complete test process [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]](/img/f7/d9bdd667e6e34b99940b9c2ecac061.png)
Complete test process [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]

Opengauss database JDBC environment connection configuration (eclipse)

Classmate, have you heard of mot?

时间复杂度与空间复杂度解析
![[sword finger offer] 22 The penultimate node in the linked list](/img/66/630ae9762f9d87817a14cb1c96015b.png)
[sword finger offer] 22 The penultimate node in the linked list

MySQL quick start instance (no loss)

AutoRunner自动化测试工具如何创建项目-Alltesting|泽众云测试

瑞吉外卖项目(三)员工管理业务开发

面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】
随机推荐
用户界面之工具栏详解-AutoRunner自动化测试工具
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
Laravel 2020-01-01t00:00:00.000000z date conversion
1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析
Yiwenjiaohui your database system tuning
[learn FPGA programming from scratch -18]: quick start chapter - operation steps 2-6- VerilogHDL sequential circuit syntax analysis (taking the counter as an example)
[从零开始学习FPGA编程-18]:快速入门篇 - 操作步骤2-6- VerilogHDL时序电路语法分析(以计数器为例)
基于FPGA的VGA协议实现
Streaking? Baa!
[learn FPGA programming from scratch -17]: quick start chapter - operation steps 2-5- VerilogHDL hardware description language symbol system and program framework (both software programmers and hardwa
Db4ai: database driven AI
How does the taskbar under the computer display open programs
Code farming essential SQL tuning (Part 1)
Detailed explanation of MySQL binlog log and master-slave replication
【剑指Offer】22.链表中倒数第K节点
[LeetCode每日一题] |686.重复叠加字符串匹配
1267_ FreeRTOS starts the first task interface prvportstartfirsttask implementation analysis
(OJ assignment of Hunan University of science and Technology) problem g: pattern matching of strings
【剑指Offer】21.调整数组顺序使奇数位于偶数前面
时间复杂度与空间复杂度解析
