当前位置:网站首页>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


边栏推荐
- MySQL 远程连接错误解决方法
- DHCP principle and configuration
- JSP+Dao整合
- Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native
- 泛型和注解
- Pointer learning diary (V) classic abstract data types and standard function libraries
- web开发
- Chapter5 foundation of deep learning
- 线程的介绍
- ssm的整合
猜你喜欢

JMeter upload and download files

JDBC encapsulates a parent class to reduce code duplication

web开发

AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

浅谈不可转让的声誉积分NFT SBTs面临的困境

PXE efficient batch network installation

The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance

Tips for using BeanShell built-in variable prev

【sklearn】tree.DecisionTreeClassifier

1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
随机推荐
Read "effective managers - Drucker"
The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
OSS文件上传
Embedded system transplantation [3] - uboot burning and use
Tips for using BeanShell built-in variable prev
SSM整合
On the dilemma faced by non transferable reputation points NFT SBTS
Reading excerpts from Liu run's "bottom logic"
What are the core strengths of a knowledge base that supports customers quickly?
JSP+Dao整合
Support complex T4 file systems such as model group monitoring and real-time alarm. e
【sklearn】tree.DecisionTreeClassifier
编译型语言和解释型语言的区别
FTP file transfer protocol
文本摘要 ACL2021
un7.23:如何在linix上安装MySQL?
Knowledge record of College Physics C in advance in summer [update]
Pointer learning diary (I)
Ia notes 2
T 6-10
