当前位置：网站首页>Detailed explanation of C language knowledge points - data types and variables  - integer variables and constants 
Detailed explanation of C language knowledge points - data types and variables  - integer variables and constants 
2022-04-23 15:02:00 【VelvetShiki_ Not_ VS】
List of articles
C In language , Every data used by the program , Its type must be specified , You must also specify the return value type of a function . Data type is an abstract form of data processed by computer to program code , The various forms of data that may appear in the computer are analyzed A classification . stay C In language , The data can be roughly divided into the following 4 Categories: ： Basic types , Pointer types , Construct type and empty type .
C The most basic data type , Its value cannot be decomposed into other types , yes C The types provided by the language library , Also known as built-in type . Common basic data types are as follows ：
Integer data is a value without a decimal part , It can be roughly divided into basic integers , Short integer and long integer （ These three categories are signed numbers ） And unsigned integers （ It also contains three ）. It can be divided into integer variables and integer constants according to storage space or value itself .
signed int ：
- Short short（ or signed short）： Occupy 2 Byte sum 16 A bit .
- Basic integers int（ or signed int）： Occupy 4 Byte sum 32 A bit .
- Long integer long（ or signed long）： Occupy 4 Byte sum 32 A bit .
- 64 Length shaping （__int64 or long long）： Occupy 8 Byte sum 64 bits .
It can be seen from the above that the occupied size of each integer variable remains unchanged under different operating system widths
Unsigned integer ：
Unsigned integers occupy the same bytes as signed integers , However, the representation range of data is different from the signed number （ Can not be negative ）.
- Unsigned short unsigned short
- Unsigned integer unsigned int
- Unsigned long unsigned long
- Unsigned 64 Length shaping unsigned int_64（ or unsigned long long）
Summary of Figure 1 ：
C Those in the language that are assigned to variables , And added a keyword before the variable declaration const The data type of is integer constant , Also called a constant variable （ But it's essentially a variable ）. Integer constants generally have the following forms ：
1. add to const Constant variable of .
2. Macro definition # define Converts the symbol defined in to a numeric constant .
3. enumeration Variable defines the actual integer constant .
const Type constant variables
Integer constants are generally written in decimal form , Hexadecimal , Octal and binary , And different hexadecimal numbers can be converted to each other .（ Refer to carry counting system conversion for details https://blog.csdn.net/qq_67611811/article/details/124256850?spm=1001.2014.3001.5502） Such a calculation method is only convenient for artificial calculation and data representation , If you want to make the computer recognize data with different base numbers , Each defined integer data constant needs to be preceded by const Keywords at the same time , The integer value to be assigned is preceded by an identifier of different base numbers ：
- Integer decimal number ： Writing numbers directly represents decimal
- Integer octal number ： Add... Before octal numbers 0
- Integer hexadecimal number ： Add... Before the hexadecimal number 0x
- Integer binary number ： Add... Before the binary string 0b
Whether it's a constant or a variable , Can be defined by type and variable identifier , The constant is initialized directly through the identification of various hexadecimal numbers / Variable , Only constants have constant properties after being initialized , Cannot be modified later ：
When using binary representation, pay attention to , adopt 0b Written All bits of binary values are numeric bits , There is no sign bit ; This is different from the signed original inverse complement .
Variables or constants of more than integer data types can be initialized by the assignment of hexadecimal numbers , Other data types , Such as Character , It can also be used as a special integer to be assigned a hexadecimal number ：
character char Type can be regarded as a special integer , Integers can also be stored and used normally , The difference lies in char Account for only a 1 byte , And only the lowest 7 Bit weights can store integers , The scope is -128~127, If it is unsigned char be 8 Bits can store values , The range is 0~255：
Looking at the median value of the graph, we can find , although char It is certain that integer values can be stored , But if The stored value exceeds the sign given above char The range of , Neither variable nor constant can store and output the value normally , What happened here char Range cycle of type number and negative boundary problem , The following modules will explain in detail .
Macros define integer constants # define
Use # define The macro definition statement implements the numeric pair identifier in Direct substitution in the precompile phase It also belongs to a kind of integer value definition ：
It should be noted that , The identifier defined by the macro cannot conflict with the local or global variables in this file , Otherwise, the compiler warns that constants cannot be assigned .
Use # define Constants can be defined repeatedly , Because the process is actually completed in the pre compilation stage , Therefore, there will be no redefinition of local or global variables ：
Be careful ： Macro definition declaration is usually defined at the beginning of a source file , As a constant, it is assigned to other variables as a value by the function in this document , But it cannot be introduced into other files （ Global variables different from static areas ）. If it is the same project file, you want to use it all # define Defined constant , You can create a header file to link the source files , In the header file, the macro defines the value corresponding to the identifier , Can be used directly in one file . Here also need to pay attention , Macro definition follows the principle of proximity , Pictured above , If the identifier defined by the macro in the header file is redefined by the macro in other files , The latest constant value shall prevail when calling the constant value in subsequent files （ Similar to the properties of global variables ）.
Enumeration type enum Defined integer constant
Enumeration type is a kind of basic data type , It can avoid repeating the tedious macro definition of the same function data constant , Here's the picture ：
Instead of repeating redundancy in the above way, use macros to define multiple constant values ,enum Is characterized by the use of a group of data with the same function , A simpler and easier way to define integer constants . Enumeration types are used when dealing with non numeric data ⼀ A numerical alternative ⽅ Law , Used to replace multiple constant sets with specific simple numbers .
The basic format ：
enum Enumeration type name
Enumeration constants 1, Enumeration constants 2,…, Enumeration constants n,
Code example ：
- enum Itself is c language ⾔ keyword ,enum Combined with type name in ⼀ They are collectively referred to as enumeration types , yes ⼀ Types （ As in the example above weekdays）. When you call this enumeration type in other functions, you need middle note ⼀ An enumeration variable to represent the enumeration type （ As defined in the above example, after enumeration weekends And defined in the main function weekdays Two enumeration variables ）.
- Enumeration constants can be initialized when the type is defined , Uninitialized enumeration constants are assigned by the first member constant by default 0 Start , From now on, it will increase in turn （ namely 0, 1, 2, …, n）.
- If the first member has an initialization value ⽽ The rest of the members did not , From the first member value to the next, it will increase automatically （ The first member is 5, Then the rest of the members 6, 7, 8, ,）; The first member is not initialized ,⽽ among ⼀ Member initialization , Then the initialization member will automatically increase in sequence （ If you have any n Members , The first member defaults to 0, The third member is 5, Then the enumeration is 0, 1, 5, 6, 7, …）.
- C In language , Enumeration types are treated by the compiler as integers int Or unsigned integer unsigned int To deal with the , Its members are integer constants , Therefore, it is usually impossible to loop through its members , Only when its integer member constant is continuous （ There is no value for jumping in the middle ） You can ask questions from beginning to end .
- Enumeration is compared to define The advantage of is to increase the readability and maintainability of the code , The former is more conducive to the management and modification of data ,⼀ You can define multiple constants at a time and assign specific values or default auto increment state , And it itself is ⼀ Types , More rigorous , It can prevent ⽌ Name pollution .⽽ Although the latter repeatedly defines multiple define The same can be achieved , But it's redundant and it's not of any kind , Give the same name for modifying the entire code ⽽⽣, Not applicable to the use of some codes .
- Each member of an enum type can be thought of as ⼀ A member list of integer constants （ Because it starts from the first member by default 0,1,2,3…）, So for enum Enumeration type calculation ⼤ Xiao yewei 4 byte .
- Enumeration type names can be omitted , However, the enumeration variable at this time can only be defined after its declaration , And multiple... Can be declared at the same time ：
This example declares enum Because there is no type , So if you don't create variables after declaration , In subsequent function calls, you cannot create new variables of this enumeration type .
- Attention is also needed , A constant of an enumeration type cannot be assigned a value in a function , You can only assign their values to other variables ; And other variables in the function （ Especially global variables ） It's best not to duplicate their identifiers , Will cause the principle of proximity .
Exclusive notes carefully organized , Code combat , It's not easy to create . If you have any questions, please leave a comment or private letter , If it helps you, please leave some likes , Collect and then go ~
本文为[VelvetShiki_ Not_ VS]所创，转载请带上原文链接，感谢
- Go basic reflection
- Practice of unified storage technology of oppo data Lake
- How to write the keywords in the cover and title? As we media, why is there no video playback
- Epolloneshot event of epoll -- instance program
- Select receives both normal data and out of band data
- The win10 taskbar notification area icon is missing
- Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Redis master-slave synchronization
Introduction to Arduino for esp8266 serial port function
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
8.3 language model and data set
Is asemi ultrafast recovery diode interchangeable with Schottky diode
8.4 realization of recurrent neural network from zero
JS -- realize click Copy function
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
We reference My97DatePicker to realize the use of time plug-in
I/O复用的高级应用：同时处理 TCP 和 UDP 服务
UML project example -- UML diagram description of tiktok
LeetCode 练习——396. 旋转函数
Leetcode149 - maximum number of points on a line - Math - hash table
Set onedrive or Google drive as a drawing bed in upic for free
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Model location setting in GIS data processing -cesium
I/O复用的高级应用之一：非阻塞 connect———使用 select 实现（也可以用 poll 实现）
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
Little red book timestamp2 (2022 / 04 / 22)
1n5408-asemi rectifier diode