1. output variables of various integer types To output different types of integers, you need to use a format qualifier that is not used. Output an integer of type unsigned int, with %u . Output long with %ld; If you want to output in hexadecimal or octal form, use %lx(or %lx) or %lo. Note: Although the suffix of an integer constant does not matter in uppercase or lowercase English letters, they must be in lowercase with the format qualifier! If we want to output a short type of integer, you can prefix h, or %hd, in the middle of%d,%ho and %hx(or %hx ) represents output in eight or hexadecimal notation, respectively. The prefix h and L can be combined with u to represent the output unsigned integer. For example:%lu represents the output unsigned long integer;%hu represents the output unsigned short type integer. If your compiler supports C99, you can use %lld and %llu respectively to represent the output long long and unsigned long. Let's look at a program that outputs various types of integers: #include <stdio.h> int main (void) { unsigned int un = 3000000000; / * I am using the compiler int is 32-bit * / Short end = 200; * * and Short is 16-bit Long big = 65537; printf ("un =%u and not%d\n", un, un); printf ("end =%hd and%d\n", end, end); printf ("big =%ld and not%hd\n", big, big); printf ("Press ENTER to quit ..."); GetChar (); return 0; } Run this program using dev-c++ compilation The output is as follows: UN = 3000000000 and not-1294967296 End = 200 Big = 65537 and not 1 Press ENTER to quit ... This program indicates that using a format qualifier incorrectly can result in unexpected output. First, the error uses%d to do the format qualifier for the unsigned integer variable un, resulting in a negative number. This is because my computer uses the same binary form to represent 3000000000 and 129496296, and the computer only knows binary . So, if we use%u to tell printf to output unsigned integers, that's 3000000000, and if we misuse%d, then the output is a negative number. However, if we change the code 3000000000 to 96, the output will not be abnormal. Since 96 does not exceed the representation range of Int. Then, for the second printf, regardless of whether we use%HD or%d, the output results are the same. This is because the c language standard stipulates that when the short type value is passed to the function, it is automatically converted to the int type value. It is converted to int because int is designed to be the most efficient integer type of computer processing. So, for computers with a short and int size, converting the variable end to an int type and passing it to a function is faster. So, H seems to have no meaning. Fact We can use%HD to see what happens when a larger integer type is truncated to a short type. and the third printf, which causes the output to be 1 due to misuse of%HD. This is because if long is 32 bits, the binary form of 65537 is 0000 0000 0000 0001 0000 0000 0000 0001, and the%HD command printf outputs the short type value, which causes printf to only pair 16-bit processing, resulting in an output of 1. in the previous tutorial, we said that it is our responsibility to ensure that the number of format qualifiers is consistent with the number of parameters. Similarly, it is our responsibility to ensure that the type and parameter types of the format qualifiers are consistent ! As mentioned above, using a format qualifier incorrectly can result in unexpected output! 2. integer Overflow First, look at the following programs: #include <stdio.h> int main (void) { /* 32-bit int denotes the upper and lower limits of the range * * int i = 2147483647, j =-2147483648; unsigned int k = 4294967295, L = 0; printf ("%d%d%d%d\n", I, I+1, J, J-1); printf ("%u%u%u%u\n", K, K+1, k+2, L, L-1); printf ("Press ENTER to quit ..."); GetChar (); return 0; } Run this program using dev-c++ compilation The output is as follows: 2147483647-2147483648-2147483648 2147483647 4294967295 0 1 0 4294967295 Press ENTER to quit ... In this program, i+1 is a negative number, j-1 is a positive number, k+1 is the 0,L-1 is 4294967295. This is because, after the addition and subtraction operations, their values are beyond the range of the type of integers they correspond to, we call this behavior overflow . If the value of the unsigned int variable exceeds the upper bound, it returns 0 and then increases from 0. If less than 0, the upper bound of the unsigned type is reached, and then the upper limit is reduced. It's like a man running around the runway, circling around and back to the starting point. If an int variable overflows, it becomes a negative number, or a positive number. For integers of type unsigned, they overflow with the same conditions as described above, which is the standard rule. But the standard does not specify what happens when a signed integer overflows. This is the most common case of a signed integer overflow that is described here, but different situations may occur on other computers using other compilers. |