当前位置:网站首页>关于数据在内存中存储的相关例题
关于数据在内存中存储的相关例题
2022-06-25 12:35:00 【LIn_jt】
关于数据在内存中的存储例题
本文将讲解数据在内存中存储的相关例题
请看下面几道例题:>
例题一:
#include <stdio.h>
int main()
{
char a = -1;
signed char b = -1;
unsigned char c = -1;
printf("a=%d,b=%d,c=%d", a, b, c);
return 0;
}
输出的结果为:>
整数在内存中存储4个字节, 而将-1存放在char类型的变量中,必然会发生截断, 因此,-1 在这三个变量中都是存放的这样的数据:

而我们打印的时候,以整型的形式来打印, 而这三个变量都为char类型, 因此,这三个变量会发生整型提升, 而整型提升是根据变量的类型来进行的。a进行整型提升时, char类型在vs编译器下默认为signed char类型, 因此, 上面全一序列中,最高位为符号位, 因此整型提升时, 前面补1,得到的序列为:
而打印的时候,以%d的形式来打印, 即以有符号的整型来打印, 因此,上面的全一序列最高位被认为是符号位,为负数, 因此,我们需要将其转化为原码之后进行打印, 转化后也即为-1,因此, 便打印出了-1, 而变量b也是相同的道理。
而c呢?,c与a, b不同的原因便出在整型提升时,变量c的类型为unsigned char, 因此,在整型提升的时候, 其八个1的二进制序列前面会补0,而不会补1.

例题2:
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n",a);
return 0;
}
输出的结果为:>

分析过程如下:>

也就与在vs环境底下跑出来的数据一样了。
现在我们思考一下, 如果把这里的char a = -128 改为char a = 128, 结果又是怎么样的呢?即
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0;
}
其实结果也是和上面 一样的, 因为不管怎么样, 此时a中存放的二进制序列也为10000000, 因此分析结果与上题一样, 得到的结果也会与上题一样。
例题三:>
#include <stdio.h>
int main()
{
int a = -20;
unsigned int b = 10;
printf("%d\n", a + b);
return 0;
}
输出的结果为:>

这时就有同学会说, 哎呀, 这不是很简单的 -20 + 10 = -10吗, 其实,真正的计算结果是这样的:>

这就是计算机真正的计算过程。
下面我们来看例题4:>
#include <stdio.h>
int main()
{
unsigned int i;
for(i = 9; i >= 0; i--)
{
printf("%u\n",i);
}
}
程序输出的结果为:> 死循环
其实很好理解, 因为变量i为unsigned int, 无符号整型数据是必然>=0的。因此, 程序运行的结果为死循环。
例题5:>
int main()
{
char a[1000];
int i;
for (i = 0; i < 1000; i++)
{
a[i] = -1 - i;
}
printf("%d", strlen(a));
return 0;
}
输出的结果为:>
此处,我们需要了解到的是char类型存储数据的最大值和最小值, 因为char类型为一个字节,因此我们可以这样来理解
即:>


因此,对于上面的程序, 我们可以这么理解:.

因此就是我们的255了。
例题6:>
unsigned char i = 0;
int main()
{
for (i = 0; i <= 255; i++)
{
printf("hehe\n");
}
return 0;
}
运行的结果为死循环:>

为什么为死循环呢?其实是因为unsigned char 类似的数据范围在0 ~255之间, 因此永远符合循环条件,进入循环。
边栏推荐
- Drawing cubes with Visio
- list.replace, str.append
- 买基金在哪里开户安全?还请赐教
- Sword finger offer II 028 Flatten multi-level bidirectional linked list
- Introduction to string 18 lectures Collection 4
- C# 切换中英文输入法
- 词法陷阱(C)
- Koa 框架
- Sword finger offer day 1 stack and queue (simple)
- MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
猜你喜欢

Geospatial search: implementation principle of KD tree

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

解析數倉lazyagg查詢重寫優化
![[flask tutorial] flask development foundation and introduction](/img/c4/fb80fbe6b563e3b304d59623ef6465.jpg)
[flask tutorial] flask development foundation and introduction

How to implement a high-performance load balancing architecture?

Optimization of lazyagg query rewriting in parsing data warehouse

leetcode - 384. 打乱数组

三行代码简单修改jar包的项目代码

Elemntui's select+tree implements the search function

Seven competencies required by architects
随机推荐
Geospatial search: implementation principle of KD tree
C # switch between Chinese and English input methods
与生产环境中的 console.log 说再见
Jenkins Pipeline使用
nacos无法修改配置文件Mysql8.0的解决方法
AI assisted paper drawing of PPT drawing
ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
利用cmd(命令提示符)安装mysql&&配置环境
三行代码简单修改jar包的项目代码
leetcode - 384. 打乱数组
list. replace, str.append
《MongoDB入门教程》第01篇 MongoDB简介
Oracle backup or restore database (expdp, impdp)
Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
Module 5 (microblog comments)
[machine learning] parameter learning and gradient descent
Alibaba stability fault emergency handling process
WIN10环境下配置pytorch
Where is it safe to open an account for buying funds? Please give me your advice
Online service emergency research methodology