[C] basic data type: int usage

Source: Internet
Author: User

1. Integer int

The C language provides many integer types. The difference between these types is the size of their value range and whether they can be negative. Int Is One Of The integer types. It is generally called an integer type. In the future, both the integer type and INT type are called integer without ambiguity.

Int represents a signed integer. That 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. In general, int occupies the memory space of one word. Therefore, a 16-Bit old IBM compatible machine uses 16 bits to store integer int values. The value range is

-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 a computer that uses a 64-bit CPU, it is natural to store more int values. 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 preceding statement creates variables but does not provide them with "values )". In the previous tutorial, we have used two methods to get the variable "value ". Value assignment: cows = 500 ;. The other is to use the scanf function: scanf ("% d", & goats );. Next we will learn the third method.

3. initialize the variable

Initialization variable refers to assigning an initial value to the variable: when declaring the variable, write the equal sign (=) after 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 all Integer constants. In C, the default integer constant type is int. 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 known as the format specifier because it specifies the form in which the printf function should use to output data. The first parameter of the printf function can only be a string, which is called a 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", 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", 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. According to the standard, if the number of format delimiters is greater than the number of parameters, the behavior of the printf function is undefined. If the number of parameters is greater than the number of format delimiters, extra parameters are ignored.

6. octal (octal) and hexadecimal (hexadecimal)

In C, the default integer constant is a decimal integer. By adding a specific prefix to an integer constant, you can set it to an octal or hexadecimal integer. Prefix 0x or 0x: Set 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. Add the prefix 0 before the integer constant to indicate 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

The format qualifier % O can be used to output integers in octal format. Note: It is a lowercase letter O, not a number 0. You can use % x or % x to output integers 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", x, x );

Printf ("dec = % d; octal = % # O; hex = % # X", 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

1. Other integer types

Int Is the Basic Integer type of C language, which can meet our needs to process 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, short is called a short integer, long is called a long integer, long is called a super-long integer, and INT is called an integer. Unsigned integers are collectively referred to as unsigned integers. For example, we call unsigned short an unsigned short integer. And so on.

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.