当前位置:网站首页>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 |
边栏推荐
- 广域网技术实验
- 7.8 Ruijie online written examination
- 记录HandsomeBlog的star用户
- Influence of pre frequency division value and automatic reload value on interrupt frequency
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- Lora communication application development
- RS-485总线通信应用
- (超详尽版,不懂随时评论)Codeforces Round #804 (Div. 2)C The Third Problem
- Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)
- 最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括
猜你喜欢

记录第N次SQL异常

Ogeek meetup phase I, together with cubefs, is hot

广域网技术实验

OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)

Republishing and routing strategy of OSPF

Static routing default routing VLAN experiment

The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.

STM32 introductory tutorial lesson 2

HCIA Basics (1)

Esp8266wi fi access cloud platform
随机推荐
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
静态路由缺省路由vlan实验
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
HandsomeForum学习论坛
(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
MySQL course 1. simple command line -- simple record welcome to supplement and correct errors
The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.
mgre的全连和星型拓扑实验
7.7 sheen Xiyin written test
[explain C language in detail] this article takes you to know C language and makes you impressed
Codeforces Round #810 (Div. 2), problem: (B) Party
OSPF在MGRE环境下的实验
机械硬盘选购指南——从选购经历谈起
C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)
RS-485 bus communication application
2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
初识网页设计
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
怎么判断一个数是奇数还是偶数?