Data types in C language-integer data

Knowledge point one: Integer data type

Integer data type Default type name
[signed]int int integer
unsigned int unsigned int unsigned integer
[signed]short[int] short Short
unsigned short[int] unsigned short unsigned short
[signed]long[int] long long integer
unsigned long[int] unsigned long unsigned long integer

            The expressions in the above categories are signed integers that can represent positive and negative integers, and unsigned integers can only represent positive integers. For example, the value range of unsigned int is 0~2*32-1 .

Knowledge point 2: Integer constants

           In C language, integer constants are expressed in three forms:

           1) Decimal integer, such as 1024, consists of 0~9;

           2) An octal integer starting with 0, such as 0 123, consists of 0~7, which is equal to the decimal number 83;

           3) Hexadecimal integers starting with 0x, such as 0x 12E2, consisting of 0~9, A~F;

Knowledge point 3: Integer variables

          Since in the C language, a certain length of storage unit is allocated in the memory for an integer variable, and the bits in the storage unit only accept 0 or 1, therefore, the data of this integer variable is stored in binary form.

          Here one storage unit is equal to one byte, and one byte is equal to 8 bits.

00000000=0,00000001=1,00000010=2......11111111=255

Knowledge point 4: Storage of integer data

           In C language, numerical values ​​are expressed in the form of two's complement. Then, this involves the complement of positive integers and the complement of negative integers. Unsigned short integers cannot represent negative numbers, and in C language, they are expressed in the original code. expressed in the form of.

           Original code;   000000000000000 =0     111111111111111 =2*16-1, that is, 0~65535.

           Complement code: 0                               =+0~+32767 (2*15-1) 32768 positive numbers.

                      1                               =-1~-32768 (-2*15) 32768 negative numbers.

             a: The complement of a positive integer is the binary form of the number, that is, the complement = original code;

                   If the credit number occupies 2 storage units, find the complement of 10:

                   1) The binary form of 10 is 1010;

                   2) 10’s complement = 1010 (the first 12 zeros are usually omitted);

             b: the complement of a negative integer;

                   1) The absolute value of the data is expressed in binary form;

                    2) The highest sign bit is 1, which means a negative number (0 means an integer);

                    3) Invert the value except the sign bit and add 1;

              Example: Find the complement of -5;

              1) The binary code of 5 = 101, the original code of 5 is 0000000000000101;

              2) Because it is a negative number, the highest sign is 1, so it is 1000000000000101;

              3) Bitwise inversion is 111111111111010;

              4) Add 1 to get 1111111111111011;

Knowledge point 5: The size of integer data in memory

           The C standard does not specify the length of the storage units occupied by various types of data. It only requires sizeof(short)<=sizeof(int)<=sizeof(long). The specific requirements are determined by each compilation system.

           sizeof is an operator that measures the length of a type or variable. You can use the sizeof() function to determine how many bytes your calculation will allocate for data, such as the following code:

#include<stdio.h>
int main()
{
printf("%d,sizeof(int));
printf("%d,sizeof(short));
printf("%d,sizeof(long));
return 0;
}

Knowledge point 6: Definition of integer variables

          C language strictly follows the mandatory type definition first and then use. Through the type delimiter, you can define a variable of integer type. The definition method is as follows:

            [Type definer] identifier; such as int num ;

           A 4-byte space is used in memory to store integer data. This space is identified by the name num .

Knowledge point 7: Overflow of integer data

          1) Only integer (including character) data can be added with the signed or unsigned modifier, but real type data cannot be added;

          2) Use "%u" format to output unsigned integer data. %u means output in unsigned decimal format. like:

unsigned short price=50;          //定义price为无符号短整型变量
printf("%u\n",price);             //指定用无符号十进制数的格式输出

          3) For a signed type of data, since the range of data that it can represent has been defined, if you continue to perform operations such as "adding" on it, it will exceed the boundary. The C language calls it data overflow, although data overflow will not An error is reported, but the expected results are not obtained, for example:

#include<stdio.h>
int main()
{
int i=32767;
printf("%d",i+1);
return 0;
}

          Result: -32768 instead of 32767

          4) After specifying a variable defined as an unsigned integer, you should not assign a negative value to it, otherwise you will get wrong results; such as:

unsigned short price=-1;   \\不能吧一个负整数存储在无符号变量中
printf("%d\n",price);

          Result: 65535, not -1

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/126686397