Basic C language tutorial (my C journey started) [5]

Source: Internet
Author: User

12. Basic data type: INTEGER (lower)

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.

13. identifier naming rules

1. length limit

C89 stipulates that the compiler should be able to handle at least31Characters (including 31)Internal identifier(Internal identifier );External identifier(External identifier), the compiler should be able to handle at least6External identifier within 6 characters. The so-calledIdentifierIs the name of a variable, macro, or function. For example, int num; in this statement, num is an identifier.
The latest C99 standard stipulates that the compiler should be able to handle at least63Internal identifier within 63 characters; the compiler should be able to process at least31External identifier within 31 characters.
In fact, we can use characters that exceed the maximum number of characters to name the identifier, but the compiler willIgnore extra characters. That is to say, if we use 35 characters to name variables, and the compiler can only process a variable name with a maximum of 31 characters, the four extra characters will be ignored by the compiler, only the first 31 characters are valid. Some ancient compilers can only process identifiers within 8 characters. For such compilers, the identifiers kamehameha and kamehameko are equivalent because they have the same first eight characters.

2. Available characters and combination rules

According to the standard, the identifier can only beUppercase/lowercase English letters,Underline(_), AndArabic numerals. The first character of an identifier must be a combination of uppercase and lowercase English letters or underscores (_), rather than a number.

Legal name Invalid name
Wiggles contains invalid characters $ Z] **/* both contain invalid characters $,] and * both are invalid characters */
Cat2 2cat/* cannot start with a number */
Hot_Tub Hot-Tub/*-invalid character */
TaxRate tax rate/* No space */
_ Kcab don't/* 'is an invalid character */

The identifier of the operating system and the C-language standard library generally starts with an underscore. Therefore, we should avoid using underscores as the beginning of the identifier we define.
C is a case-sensitive language. That is to say, star, Star, sTar, and stAr are mutually sensitive.DifferentIdentifier.
We cannot useKeywordsAndReserved identifierTo name custom variables. For keywords and reserved identifiers, clickKeyword and retained identifier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.