当前位置:网站首页>C language - value range of data type and basic data type
C language - value range of data type and basic data type
2022-07-27 02:22:00 【Lydialyy】
Catalog
5. Value range of basic data type
One 、 data type

1. Basic types
(1) Integer types
- short int
- int
- long int
- long long int
(2) Floating point type
- float
- double
- long double
(3) Character type
- char
(4) Boolean type
- _Bool
(5) Enumeration type
- enum
2.sizeof Operator
sizeof Operator is used to obtain the length of a data type or expression
Such as :-sizeof(object); //sizeof( object )
-sizeof(type_name); //sizeof( type )
-sizeof object; //sizeof object
The code for :
#include <stdio.h>
int main()
{
int i;
char j;
float k;
i = 123;
j = 'C';
k = 3.14;
printf("size of int is %d\n",sizeof(int));
printf("size of i is %d\n",sizeof(i));
printf("size of char is %d\n",sizeof(char));
printf("size of j is %d\n",sizeof j);
printf("size of float is %d\n",sizeof(float));
printf("size of k is %d\n",sizeof k);
return 0;
} Code run results :

Print the bytes occupied by each type separately :
#include <stdio.h>
int main()
{
printf("int = %d\n",sizeof(int));
printf("short int = %d\n",sizeof(short));
printf("long int = %d\n",sizeof(long));
printf("long long int = %d\n",sizeof(long long));
printf("char = %d\n",sizeof(char));
printf("_Bool = %d\n",sizeof(_Bool));
printf("float = %d\n",sizeof(float));
printf("double = %d\n",sizeof(double));
printf("long double = %d\n",sizeof(long double));
return 0;
}Running results ( The equipment is different , The results may be different ):

3.signed and unsigned
signed: Stands for signed , The first bit represents positive and negative , The remaining represents the size , for example :signed int The size range is -128-127.( The default is signed)
unsigned: Represents an unsigned , All bits are size , No negative number , for example :unsigned int The size range is :0-255.
- [signed] short [int]
- unsigned short [int]
- [signed] int
- unsigned int
- [signed] long [int]
- unsigned long [int]
- [signed] long long [int]
- unsigned long long [int]
The code for :
#include <stdio.h>
int main()
{
short i;
unsigned short j;
i = -1;
j = -1;
printf("%d\n",i);//d For printing signed
printf("%u\n",j);//u For printing unsigned
return 0;
}Running results :

so , Print j when , The result is not what we expected -1, It is 65535, This is related to the value range of the data type .
Two 、 Value range of basic data type
1. bits
CPU The smallest unit that can be read is : bits ,bit,b. Each bit can only store binary numbers , namely 0 and 1.
2. byte
Minimum addressing unit of memory mechanism : byte ,Byte,B
notes :1Byte = 8 bit
The maximum number that can be stored in a byte (
) Expressed in binary as :
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
The number is converted to decimal :2147483647
Add : Base number

Print
, The code is as follows :
# include <stdio.h>
#include <math.h>
int main()
{
unsigned int result = pow(2,32) - 1;
printf("result = %u\n",result);
return 0;
}Running results :

Be careful : If not add “unsigned”, The output is 2147483647. This is because by default int yes signed Type of , That is to say, the first bit on the left is the sign bit , So it must be added “unsigned” Make it an unsigned integer variable , bring 32 Bits are used to store values .
3. Sign bit
- Deposit signed Type of storage unit , The first bit on the left indicates the sign bit . If the bit is 0, Indicates that the integer is a positive number ; If the bit is 1, Indicates that the integer is a negative number .
- One 32 Bit integer variable , Remove the first sign bit on the left , The only remaining values are 31 A bit .
4. Complement code
- The complement of a positive number is the binary form of the number .
- The complement of negative numbers needs to be obtained through the following steps :
(1) First obtain the binary form of the absolute value of the number
(2) And then 1 The value of step is inversed by bit ( Except the sign bit )
(3) Finally, the second 2 Step value plus 1
for example :

5. Value range of basic data type
| data type | Number of bytes | Value range |
| char | 1 | -128~127 |
| unsigned char2 | 1 | 0~255 |
| short | 2 | -32768~32767 |
| unsigned short | 2 | 0~65535 |
| int | 4 | -2147483648~2147483647 |
| unsigned int | 4 | 0~4294967295 |
| long | 4 | -2147483648~2147483647 |
| unsigned long | 4 | 0~4294967295 |
| long long | 8 | -9223372036854775808~9223372036854775807 |
| unsigned long long | 8 | 0~18446744073709551615 |
| data type | Number of bytes | Value range ( The absolute value ) |
| float | 4 | 1.17549*10^-38~3.40282*10^38 |
| double | 8 | 2.22507*10^-308~1.79769*10^308 |
| long double | 12 | 2.22507*10^-308~1.79769*10^308 |
边栏推荐
- STM32入门教程第一讲
- TCP的三次握手与四次断开
- Tcp的三次握手与四次挥手
- (super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
- 7.8 Ruijie online written examination
- OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
- Dynamic routing ofps protocol configuration
- [explain C language in detail] this article takes you to know C language and makes you impressed
- 6.29 Zhong'an Summer Internship
- 7.13 Weilai approved the written examination in advance
猜你喜欢

三个整数从大到小排序(详细介绍多种方法)

7.16 written examination of Duoyi network

记录HandsomeBlog的star用户

Experiment of total connection and star topology of mGRE

OSPF静态大实验

记录第N次SQL异常

数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解

HCIA (network elementary comprehensive experimental exercise)

(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality

C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套
随机推荐
Nb-iot access to cloud platform
ESP8266Wi-Fi接入云平台
Dynamic routing rip protocol experiment
mgre的全连和星型拓扑实验
全网显示 IP 归属地,是怎么实现的?
Lora通信应用开发
数字集成电路:MOS管器件章(二)
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
怎么判断一个数是奇数还是偶数?
JVM面试题(面试必备)
Golang中的错误处理
今天浅讲一下转义字符【萌新版】
Experiment of total connection and star topology of mGRE
(super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
7.16 written examination of Duoyi network
C语言——第一个程序、打印、变量和常量
Tcp的三次握手与四次挥手
勤写标兵——云小井
机械硬盘选购指南——从选购经历谈起