当前位置:网站首页>c语言---2 初识数据类型
c语言---2 初识数据类型
2022-06-10 17:31:00 【要努力丫!】
1、前情提要
我们知道计算机语言是用来写程序的,写程序是为了解决生活中的问题的。要解决生活中的问题,就必须要有能力描述生活中的问题。比如说,要写一个线上的“购物商城”,那就需要“上价商品以及价格”,要有能力描述价格,比如说一桶泡面6块钱,一袋泡面2.5元,如何来描述整数和小数呢?分别用整型和浮点数来描述。
2、C语言中有哪些数据类型呢?每种数据类型的大小是多少?
char //字符数据类型
short //短整型
int //整型
long //长整型
long long //更长的整型
float //单精度浮点数
double //双精度浮点数
- 对于字符数据类型来说,如果要将字符’a’存放起来,就需要给它开辟一个空间,用char类型创建一个空间为ch,这个空间就可以存放字符’a’
char ch = 'a'; - 短整型:
short int num =10;或者short num =10;(short int中的int可以省略) - 整型:
int age =20; - 存在这么多种数据类型,实际上是为了能够更加丰富地表达生活中的各种值。
每种数据类型的大小是多少呢?
可以用sizeof来计算,它既是关键字也是c语言中的操作符,可用来计算变量或者类型所占空间的大小。sizeof操作符的单位是字节(byte)。
#include<stdio.h>
int main()
{
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long long));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));
return 0;
}
运行结果如下:
1
2
4
4
8
4
8
D:\2022_code\bitejiuye\test_4_10\x64\Debug\test_4_10.exe (进程 23276)已退出,代码为 0。
按任意键关闭此窗口. . .
可以看到数据类型char、short、int、long、long long、float、double所占的字节个数分别为1,2,4,4,8,4,8.为什么long与int所占的字节个数一样呢?在c语言中,有个标准sizeof(long)大于等于sizeof(int)即可。
3、补充:计算机中的单位
bit—比特位(1个比特位是用来存放1个二进制位的)
byte—字节(1字节=8个比特位(bit))
1kb=1024byte
1MB=1024kb
1GB=1024MB
1TB=1024GB
1PB=1024TB
边栏推荐
- c语言---13 循环语句while
- numpy——记录
- True thesis of information system project manager in the first half of 2022
- High number_ Chapter 6 infinite series__ Absolute convergence_ Conditional convergence
- CodeCraft-22 and Codeforces Round #795 (Div. 2)
- The relationship between trees, forests and binary trees
- mmdetection之dataset类解读
- 2022上半年信息系统项目管理师论文真题
- Talk about those things about telecommuting, participate in the essay solicitation, receive the contribution fee and win the grand prize!
- js手机端复制文本到剪切板代码
猜你喜欢

mmdetection之model构建

2022上半年信息系统项目管理师论文真题

盛最多水得容器

MMdetection之build_optimizer模块解读

There is an urgent need to enrich the smart home product line. Can fluorite be crowded on the sweeping robot track?

This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue

mmdetection之dataset类解读

(CVPR 2020) RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds

Classic topics of leetcode tree (I)

线性移动棋
随机推荐
Numpy - record
关于目前CIM(BIM+GIS)行业的一些看法
mmdetection之dataset类解读
Leetcode 875. 爱吃香蕉的珂珂
LeetCode 255. Verifying preorder traversal sequence binary search tree*
Abbexa 1,3-二棕榈素 CLIA 试剂盒解决方案
CDGA|工业企业进行数据治理的六个关键点
mmcv之Registry类解读
Vim常用命令总结
传统企业在进行信息化升级的过程中,如何做好信息化顶层设计
numpy——记录
2022上半年信息系统项目管理师论文真题
电商行业转账返款方案分析
CUDA realizes efficient search - failed audit?
高数_第6章无穷级数__绝对收敛_条件收敛
美学心得(第二百三十七集) 罗国正
分享这位大神的WPF界面设计系列视频
Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
用脚本添加URP的RendererData
Can the "no password era" that apple is looking forward to really come true?