当前位置:网站首页>C进阶-数据的存储(上)
C进阶-数据的存储(上)
2022-07-06 05:15:00 【星尘不会落】
1 数据类型介绍
基本的内置类型:
char //字符数据类型
short //短整型
int //整形
long //长整型
long long //更长的整形
float //单精度浮点数
double //双精度浮点数
类型的意义:
- 使用这个类型开辟内存空间的大小(大小决定了使用范围)。
- 如何看待内存空间的视角。
1.1 类型的基本归类:
整形家族:
char
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
浮点数家族:
float
double
构造类型:
数组类型
结构体类型 struct
枚举类型 enum
联合类型 union
指针类型:
int pi;
char pc;
float pf;
void pv;
空类型:
void 表示空类型(无类型)
通常应用于函数的返回类型、函数的参数、指针类型。
2. 整形在内存中的存储
一个变量的创建是要在内存中开辟空间的。空间的大小是根据不同的类型而决定的。
接下来我们谈谈数据在所开辟内存中到底是如何存储的。
比如:int a = 20; int b = -10;
2.1 原码、反码、补码
计算机中的整数有三种表示方法,即原码、反码和补码。
三种表示方法均有符号位和数值位两部分,符号位都是用0表示“正”,用1表示“负”,而数值位负整数的三种表示方法各不相同。
原码:
直接将二进制按照正负数的形式翻译成二进制就可以。
反码:
将原码的符号位不变,其他位依次按位取反就可以得到了。
补码:
反码+1就得到补码。
正数的原、反、补码都相同。
对于整形来说:数据存放内存中其实存放的是补码。
在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;
同时,加法和减法也可以统一处理(CPU只有加法器)此外,补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路。
我们看看在内存中的存储:

2.2 大小端介绍
什么大端小端:
大端(存储)模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中;
小端(存储)模式,是指数据的低位保存在内存的低地址中,而数据的高位,,保存在内存的高地
址中。
为什么有大端和小端:
为什么会有大小端模式之分呢?这是因为在计算机系统中,我们是以字节为单位的,每个地址单元都对应着一个字节,一个字节为8bit。但是在C语言中除了8 bit的char之外,还有16 bit的short型,32 bit的long型(要看具体的编
译器),另外,对于位数大于8位的处理器,例如16位或者32位的处理器,由于寄存器宽度大于一个字节,那么必然存在着一个如何将多个字节安排的问题。因此就导致了大端存储模式和小端存储模式。
例如:一个 16bit 的 short 型 x ,在内存中的地址为 0x0010 , x 的值为 0x1122 ,那么 0x11 为高字节, 0x22 为低字节。对于大端模式,就将 0x11 放在低地址中,即 0x0010 中, 0x22 放在高地址中,即 0x0011 中。小端模式,
刚好相反。我们常用的 X86 结构是小端模式,而 KEIL C51 则为大端模式。很多的ARM,DSP都为小端模式。有些ARM处理器还可以由硬件来选择是大端模式还是小端模式。

边栏推荐
- 2021RoboCom机器人开发者大赛(初赛)
- 用StopWatch 统计代码耗时
- 图数据库ONgDB Release v-1.0.3
- Sliding window problem review
- Crazy God said redis notes
- 饼干(考试版)
- Postman test report
- Vite configures the development environment and production environment
- Compilation and connection of shader in games202 webgl (learn from)
- Three methods of Oracle two table Association update
猜你喜欢

Leetcode dynamic planning day 16

RT thread analysis log system RT_ Kprintf analysis

Postman关联

Cve-2019-11043 (PHP Remote Code Execution Vulnerability)

GAMES202-WebGL中shader的编译和连接(了解向)
![[mathematical modeling] differential equation -- sustainable development of fishing industry](/img/7c/2ab6f2a34bc2c97318537ec8e0b0c5.png)
[mathematical modeling] differential equation -- sustainable development of fishing industry
![[lgr-109] Luogu may race II & windy round 6](/img/fe/d5b67c7dff759c519a04da023630ea.png)
[lgr-109] Luogu may race II & windy round 6

Compilation and connection of shader in games202 webgl (learn from)

Golang -- TCP implements concurrency (server and client)

Idea one key guide package
随机推荐
nacos-高可用seata之TC搭建(02)
2021 robocom world robot developer competition - undergraduate group (semi-finals)
Why does MySQL need two-phase commit
JS quick start (II)
Weng Kai C language third week 3.1 punch in
Crazy God said redis notes
GAMES202-WebGL中shader的编译和连接(了解向)
Using stopwatch to count code time
Fuzzy -- basic application method of AFL
组播和广播的知识点梳理
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Request (request object) and response (response object)
The underlying structure of five data types in redis
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
flutter 实现一个有加载动画的按钮(loadingButton)
February 12 relativelayout
Implementing fuzzy query with dataframe
[effective Objective-C] - memory management