Hexadecimal number followed by L/U/UL analysis

C language supports two different numeric types, integer type (also called integer), and floating point type (also called floating point). The value of the integer type is an integer, and the value of the floating type may have a fractional part.

Integer type classification

Integer types are divided into two categories: signed and unsigned.

If the signed integer is positive or zero, the leftmost bit (sign bit) is 0; if it is negative, the sign bit is 1. Therefore, the binary representation of the largest 16-bit integer is 0111 1111 1111 1111, and the corresponding value is 32767 (that is, 2^15-1). The largest 32-bit integer is 0111 1111 1111 1111 1111 1111 1111 1111, and the corresponding value is 2147483647 (that is, 2^31-1).

Unsigned integers (the leftmost bit is part of the value) integers are called unsigned integers. The largest 16-bit unsigned integer is 65535 (ie 2^16-1), and the largest 32-bit unsigned integer is 4294967295 (ie 2^32-1).

By default, integer variables in the C language are all signed, which means that the leftmost bit is reserved for the sign bit. To tell the compiler that the variable has no sign bit, you need to declare it as an unsigned type. Unsigned integers are mainly used for system programming and low-level machine-related applications.

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 (can be abbreviated as short)

2) int

3) long int (abbreviation: long)

4) long long int (abbreviation: long long)

5) unsigned short int(简写:unsigned short)

6) unsigned int

7) unsigned long int(简写:unsigned long)

8) unsigned long long int(简写:unsigned long long)

Whether or not signed means a signed integer, for example, signed int is equivalent to int.

Generally, we call short as short integer, long as long integer, long long as super long integer, and int as integer. Those integer types beginning with unsigned are collectively called unsigned integer types. For example: We call unsigned short an unsigned short integer, and so on.

Length of integer data

C language only stipulates short <= int <= long int. It depends on the specific compiler. The long int type is not necessarily 64-bit. In many cases, long int and int indicate the same range.

In a 16-bit operating system (such as DOS), 2 bytes are generally used to store an int type of data; in a 32-bit operating system (such as Windows98), the default is 4 bytes. The length and read-write format of various integer data are shown in the following table:

Integer constant

Constants are numbers that appear as text in the program, not numbers that are read, written, or calculated. C language allows writing integer constants in decimal (base 10), octal (base 8) and hexadecimal (base 16) forms.

Octal numbers are written with numbers 0~7. Each digit of an octal number represents a power of 8 (this is the same as each digit of a decimal number represents a power of 10). Therefore, the octal number 237 expressed as a decimal number is 2x8^2+3x8^1+7x8^0=128+24+7=159.

Hexadecimal numbers are written with numbers 0-9 plus letters A-F, where letters A-F represent numbers from 10-15. Each digit of the hexadecimal number represents a power of 16. The decimal value of the hexadecimal number 1AF is 1x16^2+10x16^1+15x16^0=256+160+15=431.

Decimal constants contain numbers from 0 to 9, but they must not start with zero: 15 255 32767

The octal constant contains only the numbers from 0 to 7, and must start with a zero: 017 0377 077777

Hexadecimal constants contain numbers from 0 to 9 and letters from a to f, and always start with 0x: 0xf 0xff 0x7fff

The letters in the hexadecimal constant can be either uppercase or lowercase letters: 0xf 0xFF 0x7fFF

Octal and hexadecimal are just ways to write numbers, they do not affect the actual storage of numbers. Integers are stored in binary form, regardless of the representation. You can switch from one writing method to another at any time, or even mix them together: the value of 10+015+0x20 is 55 (decimal).

The type of a decimal integer constant is usually int, but if the value of the constant is too large to be stored in the int type, the long int type is used. If long int is not enough, use unsigned long int as the last try. When determining the rules for octal and hexadecimal constants, the compiler will try int, unsigned int, long int, and unsigned long int in sequence until it finds a type that can represent the constant.

C99's rules for determining the type of integer constants are somewhat different from C89. For a decimal constant without a suffix (U, u, L, l, LL, ll), its type is the "smallest" type of int, long int or long long int that can represent the value. For octal or hexadecimal constants, the possible type order is int, unsigned int, long int, unsigned long int, long long int, and unsigned long long int. Any suffix after the constant changes the list of possible types. For example, the type of a constant ending in U (or u) must be one of unsigned int, unsigned long int, and unsigned long long int.

In order to force the compiler to treat constants as long integers, just add a letter L (or l) to the end:

15L 0377L 0x7fffL

To indicate that it is an unsigned constant, you can add the letter U (or u) after the constant:

15U 0377U 0x7fffU

L and U can be used in combination to indicate that the constant is both long and unsigned: 0xffffffffUL. (The order and capitalization of the letters L and U do not matter.)

In C99, integer constants ending in LL or ll (two letters must be the same in capitalization) are of type long long int. If the letter U (or u) is added before or after LL or ll, the integer constant is of type unsigned long long int.

Integer overflow

When performing arithmetic operations on integers, the result may be too large to represent. For example, when performing arithmetic operations on two int values, the result must still be represented by the int type; otherwise (too many digits are required to indicate the result) overflow will occur.

The behavior of integer overflow depends on whether the operand is signed or unsigned. When an overflow occurs in a signed integer operation, the behavior of the program is undefined, and the result of the undefined behavior is uncertain. The most likely situation is that only the result of the calculation is wrong, but the program may also crash or other unexpected situations.

When an overflow occurs during an unsigned integer operation, the result is defined: the correct answer is modulo 2^n, where n is the number of bits used to store the result. For example, if you add 1 to the unsigned 16-digit number 65535, The result can be guaranteed to be zero.

Integer read/write

Reading and writing unsigned integers, short integers, and long integers require conversion specifiers.

When reading and writing unsigned integers, use the letters u, o, or x instead of d in the conversion description. If you use the u specifier, the number will be read and written in decimal form, o means octal form, and x means hexadecimal form.

 
  1.  
  2. unsigned int u;

  3.  
  4. scanf("%u", &u); /* reads u in base 10 */

  5. printf("%u", u); /* writes u in base 10 */

  6. scanf("%o", &u); /* reads u in base 8 */

  7. printf("%o", u); /* writes u in base 8 */

  8. scanf("%x", &u); /* reads u in base 16 */

  9. printf("%x", u); /* writes u in base 16 */

When reading and writing short integers, add the letter h before d, o, u, or x:

 
  1.  
  2. short s;

  3.  
  4. scanf("%hd", &s);

  5. printf("%hd", s);

When reading and writing long integers, add the letter l before d, o, u, or x:

 
  1.  
  2. short l;

  3.  
  4. scanf("%ld", &l);

  5. printf("%ld", l);

When reading and writing long integers (C99 only), add the letter ll before d, o, u or x:

 
  1.  
  2. short ll;

  3.  
  4. scanf("%lld", &ll);

  5. printf("%lld", ll);

转载:https://blog.csdn.net/slj_win/article/details/53422465

Guess you like

Origin blog.csdn.net/jfyes/article/details/88844334