1. Other integer types Int Is in C.Basic Integer typeTo meet our needs for processing general data. The C language also provides four keywords that can modify int: short, long, signed, and unsigned. Using these four keywords, the C language standard defines the following integer types: 1) short int (which can be abbreviated as short). Like int, it is also a signed integer. 2) long int (Abbreviation: long), signed integer 3) long int (Abbreviation: long), type added to the C99 standard, Signed integer 4) unsigned int (abbreviated as unsigned). It is an unsigned integer and cannot be a negative number. 5) unsigned long int (Abbreviation: unsigned long), unsigned integer, It cannot represent a negative number. 6) unsigned short int (Abbreviation: unsigned short), unsigned integer, It cannot represent a negative number. 7) unsigned long int (Abbreviation: unsigned long ), Type added to C99, unsigned integer 8) All Integer types without unsigned are signed integers by default. Adding signed before these integer types can give readers a clearer understanding. These are signed integers, although signed represents signed integers. For example, signed int is equivalent to int. Generally, we call shortShort integerLong is calledLong IntegerLong is calledUltra-long integerInt is calledInteger. The integer types of unsigned headers are collectively referred toUnsigned integer. For example, we call unsigned shortUnsignedShort integer. And so on. 2. Declaration Method The declarations of these integer types are the same as those of the int type. For example: Long int estine; Long johns; Short int erns; Short ribs; Unsigned int s_count; Unsigned players; Unsigned long headcount; Unsigned short yesvotes; Long ago;/* C99 special */ Unsigned long ego;/* C99 specific */ If your compiler does not support the C99 standard, you cannot use long and unsigned long. 3. value range (indicating range) The standard also specifiesMinimum value range. Short has the same minimum expression range as int, which is-32767 to 32767. That is,-(2 ^ 15-1) to (2 ^ 15-1 ). 2 ^ 15 indicates the 15th power of 2. Similarly, the 20 power of 2 is recorded as 2 ^ 20, and so on. Note: 2 ^ 15 in C does not represent the 15th power of 2. For ease of writing, let's say this. The minimum value range of long is-2147483647 to 2147483647. That is,-(2 ^ 31-1) (2 ^ 31-1 ). The minimum value range of unsigned short is the same as that of unsigned int, which ranges from 0 to 65535 (2 ^ 16-1 ). The minimum value range of unsigned long is 0 to 4294967295 (2 ^ 32-1 ). The minimum value range of long is-9223372036854775807 (-(2 ^ 63-1) to 9223372036854775807 (2 ^ 63-1); unsigned long is 0 18446744073709551615 (2 ^ 64-1 ). According to the standard, int indicates the rangeCannot be lessShort indicates the range, and long indicates the range.Cannot be lessInt. This means that the short variable may occupy less space than the int variable, while the long variable may occupy more space than the int variable. In a 16-bit computer, int and short are generally 16 bits, while long is 32 bits. In a 32-bit computer, short is generally 16 bits, long and int are 32 bits. In TC2 (16-bit compiler), int Is 16 bits, while in Dev-C ++ (32-bit compiler), int Is 32 bits. Variables declared using unsigned int can only represent positive integers. If the int value is 16 bits, the unsigned int value ranges from 0 to 65535 (2 ^ 16-1 ). This is because unsigned does not need a signed bit. You can use all 16 digits to represent integers. Int requires a single digitSymbol bitIs used to indicate positive and negative. Only 15 digits are used to represent integers. Currently, long is usually 64-bit, long is 32-bit, short is 16-bit, and int or 16-bit, or 32-bit. The specific number of BITs used by a compiler to indicate these types. We can useOperatorSizeof. For example: Printf ("% lu \ n", (unsigned long) sizeof (int) * 8);/* number of digits of the output int */ Printf ("% zu \ n", sizeof (short) * 8);/* Number of short output digits */ We will talk about the usage of sizeof in the future. Now we only need to have an impression. In the second sentence% ZuIt is unique to C99. If your compiler does not support C99 (to be precise, it should be true if the library function used by your compiler does not support C99), the running result will fail. 4. Select Integer type If you want to handle onlyPositive Integer, The unsigned integer type should be given priority. If the integer to be processed exceeds the range expressed by the int and the long value in your compiler is larger than the int value, the long value is used. However, if not necessary, try not to use long because it may reduce the program running efficiency. NOTE: If both long and int are 32-bit in your compiler and you need to use a 32-bit integer, use long instead of int. Only in this way can our program be securely transplanted to a 16-bit computer, because in a 16-bit computer, int Is usually 16-bit. Similarly, if you want to use a 64-bit integer, use long. If the int value is 32 bits, using short can save space. However, make sure that the integer to be processed does not exceed the short value range. This "save" is meaningless for computers with large memory.
5. long constants and long Constants Generally, Integer constants are stored as int type. If the integer constant we use exceeds the int expression range, the C language requires the compiler to automatically use the unsigned int to process this constant. If unsigned is not enough to represent this constant, the compiler will use long. If not, use unsigned long, long, unsigned long in sequence. If unsigned long does not mean it, then the compiler will have nothing to do with it. Note: long and unsigned long are unique to C99. For example, if the int value is 16 bits, it cannot represent the constant 1000000. The compiler uses long to process this constant, because the unsigned int cannot represent 1000000. Likewise, hexadecimal and octal Integer constants are usually processed as int. However, when the constants we use exceed the int expression range, the compiler uses unsigned int, long, unsigned long, long, and unsigned long in sequence. Until the type used is sufficient to represent that constant. Sometimes we use a small constant, but we want this constant to be processed as a long value. This requires the constant to be followedSuffixL (lower case letter l) or L (upper case letter L ). We should avoid using l, because l is easy to confuse with number 1. For example, an integer constant 7 is processed as an int, but an integer constant 7L (or 7l) is processed as a long. Similarly, after an integer constant is appended with the suffix ll or LL, the constant will be treated as long. Example: 3LL. To use an unsigned integer constant, use the suffix u or U. For example, 2u, 3U, 4Lu, 5ul, 6LU, 7LLU, 8Ull, and 9uLL. These suffixes can also be used for hexadecimal and octal Integer constants. Example: 020L, 010LL, 0x30uL, 0x40ull. |