当前位置:网站首页>C语言进阶篇 一.数据的存储
C语言进阶篇 一.数据的存储
2022-07-24 05:16:00 【且随疾风前行->】
目录
1. 数据类型详细介绍
整型类型:
(每种整型类型都可分为无符号和有符号两种类型,不定义unsigned时默认是无符号的)
字符型类型:
float 4字节,单精度
double 8字节,双精度
long double(C99引入)12字节
其它类型:
构造类型:
数组类型 int a[3]的类型是 int [3]
结构体类型 struct
枚举类型 enum
2. 整形在内存中的存储:原码、反码、补码
#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;
}a: -1是整数存储在char发生截断内存中存的是1111 1111,以%d打印时发生整形提升以原类型(有符号类型)提升,左补1为11111111 11111111 11111111 11111111 转为原码是10000000 00000000 00000000 00000001 故打印a=-1,a,b和类型等价故都是-1。
c:-1存在c中同a,但是在以%d打印时整形提升(无符号类型)为00000000 00000000 00000000 11111111 是个正数,原反补相同故打印255.
例题2.
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n",a);
return 0;
}-128是10000000 00000000 00000000 10000000 转为补码是11111111 11111111 11111111 11111111存在a截断为11111111,以%u形式打印,整形提升为11111111 11111111 11111111 11111111无符号数原反补相同,故打印4294967295。
其他例题:
3.
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0;
}
//128存在a中是1000 0000 ,以%u打印
//先整形提升为11111111 11111111 11111111 10000000
//无符号数原反补相同,直接打印4294967168
4.
int i= -20;
unsigned int j = 10;
printf("%d\n", i+j);
//按照补码的形式进行运算,最后格式化成为有符号整数
//-20原码是 10000000 00000000 00000000 0001 0100
//-20补码是 11111111 11111111 11111111 1110 1100
//10补码是 00000000 00000000 00000000 0000 1010
//补码运算得11111111 11111111 11111111 1111 0110
//转为原码 10000000 00000000 00000000 0000 1010
//故打印-10
5.
unsigned int i;
for(i = 9; i >= 0; i--) {
printf("%u\n",i);
}
//当i=0时,i--,因为i是无符号整形,
//故变成11111111 11111111 11111111 11111111
//是unsigned int类型的最大值,故这段代码将死循环
6.
int main()
{
char a[1000];
int i;
for(i=0; i<1000; i++)
{
a[i] = -1-i;
}
printf("%d",strlen(a));
return 0;
}
//对于char类型,arr[i]从-1递减到-128后再递减则得127,最后递减到0然后循环往复
//\0的ASCII码值是0故打印的是128+127=255.
7.
#include <stdio.h>
unsigned char i = 0;
int main()
{
for(i = 0;i<=255;i++)
{
printf("hello world\n");
}
return 0;
}
//对于unsigned char类型的i而言从0递增到255恰好是其类型的范围,
//255再加一则是1 00000000发生截断,i又变成了0,故程序死循环
查看整形类型最值在头文件limits.h,浮点型最值=则在float.h

3. 大小端字节序介绍及判断
顾名思义是各个字节在内存中的存储顺序:
int check_sys()
{
int i = 1;
return (*(char *)&i);
}4. 浮点型在内存中的存储解析

有效数字M
指数E


边栏推荐
- Hcip day 3 - mGRE experiment
- Knowledge record of College Physics C in advance in summer [update]
- 1. There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8,... Program to sum the first 20 items of this sequence.
- 【sklearn】数据预处理
- MySQL深入了解
- NLP learning roadmap (mind map) is very comprehensive and clear!
- [basic 8] - processes and threads
- Integration of SSM
- PXE efficient batch network installation
- 【【【递归】】】
猜你喜欢

FTP file transfer protocol

Chapter5 foundation of deep learning

C primer plus learning notes - 6. Arrays and pointers

Drools development decision table

JMeter upload and download files

JSP+Dao整合

Update C language notes

Introduction to threads

Jsp+dao integration

What are the core strengths of a knowledge base that supports customers quickly?
随机推荐
The difference between compiled language and interpreted language
)的低字节来反馈给应用层或者成多种格式文档:
【sklearn】PCA
The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
安装Pytorch+anaconda+cuda+cudnn
MySQL连接
Crazy God redis notes 09
好的程序员与不好的程序员
NumPy 统计相关函数示例教程
浅谈不可转让的声誉积分NFT SBTs面临的困境
The fourth job: about the usage of cat, grep, cut, sort, uniq, VIM, TR and other commands
c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
Blue Bridge Cup 31 day sprint 21 day (C language)
泛型和注解
Source code compilation!!
Introduction to 51 single chip microcomputer (dedicated to the most understandable article for beginners)
[database connection] - excerpt from training
Hotel IPTV digital TV system solution
使用d2l包和相关环境配置的一些血泪心得
Mrs +apache Zeppelin makes data analysis more convenient
