1. Output Variables of various integer types Output Integers of different types, which need not be usedFormat qualifier. Returns an integer of the unsigned int type.% U. Output long, to use% LdIf you want to output data in hexadecimal or octal format, use% Lx(Or% LX) Or% Lo. Note: although the suffixes of Integer constants use uppercase or lowercase English letters, their format delimitersLowercase must be used! If we want to output an integer of the short type, we can add it to % d.Prefix H, That is% HdSimilarly,% HoAnd% Hx(Or% HXAre output in octal or hexadecimal format. PrefixHAndLAndUCombination, indicating the output of an unsigned integer. For example:% LuAn integer of the unsigned long type;% HuReturns an integer of the unsigned short type. If your compiler supports C99, you can use% LldAnd% LluOutput long and unsigned long respectively. Let's look at a program that outputs various types of integers: # Include<Stdio. h> Int main (void) { Unsigned int un = 3000000000;/* the compiler int I use is 32-bit */ Short end = 200;/* While 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; } UseDev-C ++The output result of compiling and running this program is as follows: Un = 3000000000 and not-1294967296 End = 200 and 200 Big = 65537 and not 1 Press ENTER to quit... This program indicates that incorrect format delimiters may cause unexpected output. First, the error uses % d as the format qualifier of the unsigned integer variable un, resulting in negative output. This is because my computer uses the same binary format to represent 3000000000 and-129496296, whileThe computer only recognizes binary. Therefore, if we use % u to tell printf to output an unsigned integer, the output is 3000000000; if we misuse % d, the output is a negative number. However, if we change 3000000000 in the code to 96, no exception will occur in the output. Because 96 is not beyond the int range. Then, for the second printf, 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 must be automatically converted to the int type value. The reason for converting to int Is that int is designed as the integer type with the highest processing efficiency on the computer. Therefore, for computers with different short and int sizes, It is faster to convert the variable end to the int type and pass it to the function. H seems meaningless. Actually not. We can use % hd to see what the larger Integer type will look like when it is truncated to the short type. The third printf causes the output to be 1 due to misuse of % hd. This is because, if long is 32-bit, the binary form of 65537 is 0000 0000 0000 0001 0000 0000 0000 0001, while the % hd command printf outputs a value of the short type, as a result, printf only processes the last 16 bits, resulting in output 1. In the previous tutorial, we said,Ensure that the number of format delimiters is consistent with the number of parameters.It is our responsibility. Similarly,Ensure that the type of the format qualifier is consistent with that of the ParameterIt is also our responsibility! As mentioned above, incorrect format delimiters can lead to unexpected output! 2. Integer Overflow First, check the following program: # Include<Stdio. h> Int main (void) { /* 32-bit int indicates the upper and lower limits of the range */ Int I = 2147483647, j =-2147483648; Unsigned int k = 4294967295, l = 0; Printf ("% d \ n", I, I + 1, j, J-1 ); Printf ("% u \ n", k, k + 1, k + 2, l L-1 ); Printf ("Press ENTER to quit ..."); Getchar (); Return 0; } Use Dev-C ++ to compile and run this program. The output result is as follows: 2147483647-2147483648-2147483648 2147483647 4294967295 0 1 0 4294967295 Press ENTER to quit... In this program, I + 1 is negative, J-1 is positive, k + 1 is 0, L-1 is 4294967295. This is because after the addition and subtraction operations, their values are beyond the corresponding integer expression range. We call this phenomenonOverflow. If the value of the unsigned int type variable exceeds the upper limit, 0 is returned and increases from 0. If it is less than 0, it will reach the unsigned upper limit and then decrease from the upper limit. It's like a person is running around the runway, circling and returning to the starting point. If the int type variable overflows, it will become a negative number or a positive number. For unsigned integers, the overflow conditions must be the same as described above. However, the standard does not specify what will happen when the signed integer overflows. The occurrence of signed integer overflow is the most common. However, in other computers, different compilers may occur. |