当前位置:网站首页>Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]

Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]

2022-04-23 15:02:00 VelvetShiki_ Not_ VS

data type

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 basic data types of language

Basic types

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 types

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 .

Integer variables

signed int

  1. Short short( or signed short): Occupy 2 Byte sum 16 A bit .
  2. Basic integers int( or signed int): Occupy 4 Byte sum 32 A bit .
  3. Long integer long( or signed long): Occupy 4 Byte sum 32 A bit .
  4. 64 Length shaping (__int64 or long long): Occupy 8 Byte sum 64 bits .
    32 The size of each integer variable under the bit operating system 123123
    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 ).

  1. Unsigned short unsigned short
  2. Unsigned integer unsigned int
  3. Unsigned long unsigned long
  4. Unsigned 64 Length shaping unsigned int_64( or unsigned long long)

Summary of Figure 1 : Insert picture description here

integer constants

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 :

  1. Integer decimal number : Writing numbers directly represents decimal
  2. Integer octal number : Add... Before octal numbers 0
  3. Integer hexadecimal number : Add... Before the hexadecimal number 0x
  4. Integer binary number : Add... Before the binary string 0b

Code display
 Insert picture description here
explain :

  1. 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 :
     Insert picture description here

  2. 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 .

  3. 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
     Insert picture description here

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:

 Insert picture description here
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
  1. 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 :
     Insert picture description here 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 .

  2. 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 :
     Insert picture description here

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 :
 Insert picture description here
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 :
 Insert picture description here
explain :

  1. 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 ).
  2. 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).
  3. 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, …).
  4. 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 .
  5. 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 .
  6. 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 .
  7. 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 :
     Insert picture description here
    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 .
  8. 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 .
     Insert picture description here

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]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411217052.html