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

Source: Internet
Author: User

10. Basic data type: INTEGER (upper)

1. Integer int

C language provides manyInteger type(Integer), the difference between these integers lies in theirValue RangeAnd whether it can be negative. Int Is One Of The integer types. It is generally called an integer type.
Int standsSigned integerThat is to say, the variable declared with int can be a positive number, a negative number, or zero, but only an integer. The minimum value range of int Is-32767 to 32767. The int value range varies with the machine, but must be greater than or equal to-32767 to 32767. Generally, int occupies oneWordMemory. Therefore,Font LengthFor a 16-bit legacy IBM compatible machine, use 16-bit to store integer int values ranging from-32768 to 32767. Currently, personal computers are generally 32-bit long. In these computers, int Is usually 32-bit, and the value range is-2147483648 to 2147483647. For 64-bit CPU computers, it is natural to use more bytes to store the int value. The value range is certainly larger.

2. Declare int type variables

As we can see in the previous tutorial, int is used to declare an integer variable: Start With int, followed by the name of the variable, and end with a semicolon. For example:

Int erns;/* declare a variable */
/* Note: you must use a comma (,) instead of a semicolon (;)*/
Int hogs, cows, goats;/* declare three variables */

The above statement creates variables, but does not provide them with"Value(Value )". In the previous tutorial, we have used two methods to get the variable "value ". One isAssignment: Cows = 500 ;. The other is to use the scanf function: scanf ("% d", & goats );. Next we will learn the third method.

3. initialize the variable

The initialization variable refers to the variableInitial Value assignment: When declaring a variable, write the equal sign (=) next to the variable name, and then write the "value" you want to assign to the variable ". For example:

Int hogs = 21;
Int cows = 32, goats = 14;
Int dogs, cats = 94;

The preceding statement creates variables, allocates space for these variables, and assigns initial values. Note that in the third row, only cats is initialized to 94, while dogs is not initialized! For example:

4. int Constants

In the above example, 21, 32, 14, and 94 are bothInteger constant. In C, the default type of Integer constants is int. That is to say, the size of memory space occupied by Integer constants is generally equal to the size occupied by int variables. If the size of an integer constant exceeds the int value range, the compiler will treat this integer constant as a long int type, which will be discussed later.
21, 32, 14, and 94 are within the int value range, so they are all int constants.

5. Output int-type data
We can use the printf function to output int-type data. As we saw in the previous tutorial, placeholder % d indicates that the output is int type data, which tells the printf function where to output the corresponding int type data. % D is also calledFormat qualifier(Format specifier), because it specifies the form used by the printf function to output data. The first parameter of the printf function can only be a string. This string is calledFormat String(Format string ). The number of % d in the format string should be provided to the printf function accordingly. Int type parameters can be int type variables, int type constants, and expressions with results of int type. For example:

Int year = 2005;/* year is an int variable */
Printf ("Today is % d-% d \ n", year, 9, 20 + 9);/* 20 + 9 is an addition expression */

It is our responsibility to ensure that the number of format delimiters is consistent with the number of parameters. The compiler is not responsible for capturing such errors! For example:

# Include<Stdio. h>

Int main (void)
{
Int ten = 10, two = 2;

Printf ("% d minus % d is % d \ n", ten);/* two parameters are missing */

Getchar ();/* Wait for the user to press ENTER */
Return 0;
}

This program can be compiled, but the running result will be unexpected, because we write less than two parameters. The first % d is replaced by the value of the ten parameter, and the other two % d will be replaced by the value originally stored in the memory. Because the stored values in the memory are uncertain, the output result is uncertain.

6. octal (octal) and hexadecimal (hexadecimal)

In C, the default integer constant isDecimal(Decimal) integer. By adding a specificPrefix, You can set itOctalOrHexadecimalInteger. Prefix0xOr0XSet the integer constant to a hexadecimal integer. Note: It's the number 0, not the letter O. Do not make a mistake! For example, the hexadecimal value of 16 in decimal format is 0x10 or 0X10. Prefix before an integer constant0Which indicates that it is an octal integer. Note that the number is 0, not the letter O. For example, if the value of 16 in decimal format is octal, the value is 020.

7. Output Data in octal or hexadecimal format

Use a format qualifier% OAn integer can be output in octal format. Note: It is a lowercase letter o, not a number 0. Use% XOr% XAn integer can be output in hexadecimal format. Lowercase x indicates that lowercase letters are used for output, and uppercase X indicates that uppercase letters are used for output. With % # o, % # x or % # X, the output will include the prefix 0, 0x, or 0X. For example:

# Include<Stdio. h>

Int main (void)
{
Int x = 200;

Printf ("dec = % d; octal = % o; hex = % x; HEX = % X \ n", x, x );
Printf ("dec = % d; octal = % # o; hex = % # x; HEX = % # X \ n", x );

Getchar ();
Return 0;
}

The output of this program is:

Dec = 200; octal = 310; hex = c8; HEX = C8
Dec = 200; octal = 0310; hex = 0xc8; HEX = 0XC8

11. Basic data type: INTEGER (medium)

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.

Related Article

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.