C Language Learning Tutorial Chapter II-Data types, operators, Expressions (ii)

Source: Internet
Author: User
Tags constant variables range

In a program, it is based on prefixes to differentiate the various numbers. Therefore, do not mistake the prefix to create incorrect results when writing constants. 4. Integer constant suffix in 16-bit word machine, the basic integer length is also 16 bits, so the range of the number of representations is also limited. The range of the decimal unsigned integer constant is 0~65535 and the signed number is -32768~+32767. The representation range of octal unsigned numbers is 0~0177777. The representation range for hexadecimal unsigned numbers is 0x0~0xffff or 0X0~0XFFFF. If the number used exceeds the above range, it must be represented by a long integer number. Long integers are represented by the suffix "l" or "L". For example:
Decimal Long integer constant 158L (decimal 158) 358000L (decimal-358000)
Octal Long integer constant 012L (decimal) 077L (decimal) 0200000L (decimal 65536)
Hexadecimal long integer constant 0x15l (decimal) 0xa5l (decimal 165) 0x10000l (decimal 65536)

The long integer 158L and the base integer constant 158 are not different numerically. But for 158L, because it is a long integer, the C-compilation system allocates 4 bytes of storage space to it. For 158, because it is a basic integer, only 2 bytes of storage space are allocated. Therefore, in the operation and output format should be paid attention to avoid errors. Unsigned numbers are also available as suffixes, and the unsigned number of integer constants is suffix "u" or "U". For example: 358u,0x38au,235lu are unsigned numbers. Prefixes, which can be used at the same time to represent various types of numbers. If 0xa5lu represents a hexadecimal unsigned long integer A5, its decimal is 165.

Integral type variable

Integer variables can be grouped into the following categories:
1. Basic type
The type specifier is int, which takes up 2 bytes in memory and takes the base integer constant.
2. Short and full amount
The type descriptor is short int or short ' c110f1. The byte and value ranges are the same as the base type.
3. Long-integer
The type specifier is long int or long, and occupies 4 bytes in memory, and its value is an integer constant.
4. Unsigned type
The type descriptor is unsigned.
The unsigned form can also be matched with the above three types:
(1) The unsigned basic type specifier is unsigned int or unsigned.
(2) unsigned short integer type descriptor is unsigned
(3) unsigned long integer type descriptor is unsigned long
The amount of memory space taken up by the various unsigned types is the same as the corresponding number of signed types. However, because the symbol bit is omitted, it cannot represent a negative number. The following table lists the range of memory bytes and numbers that are allocated for each type of integer in Turbo C.
The range of type descriptor number allocated bytes
int-32768~32767
Short int-32768~32767
Signed int-32768~32767
unsigned int 0~65535
Long int-2147483648~2147483647
unsigned long 0~4294967295
Description of Integer variable
The general form of variable description is: Type descriptor variable name identifier, variable name identifier, ...; For example:
int a,b,c; (a,b,c as Integer variable)
Long x,y; (X,y is a long integer variable)
unsigned p,q; (p,q as unsigned integer variable)

The following points should be noted when writing variable descriptions:
1. Allows multiple variables of the same type to be described after a type descriptor. The variable names are separated by commas. At least one space interval between the type descriptor and the variable name.
2. After the last variable name must end with the ";" sign.
3. The variable description must be placed before the variable is used. It is generally placed at the beginning of the function body.
[Practice]//1int a,b;
short int C;
Short d=100;
a=d-20;
B=a+d;
C=a+b+d;
D=d-a+c-b; ' Vtable
a,2,0
b,2,0
c,2,0
d,2,100
of Vtable
' Vupdate
1,0;2,0
3,0
4,100
1,80
2,180
3,360
4,200
of Vupdate
of Practice
[Practice]//2int a=5;
int b=9;
long int C;
Long D;
c=a+b-7;
D=a*b*c;
C=d*d*d;
a=c-d; ' Vtable
a,2,5
b,2,9
c,4,0
d,4,0
of Vtable
' Vupdate
1,5
2,9
3,0
4,0
3,7
4,315
3,31255875
1,-5112
of Vupdate
of Practice
[Practice]//3int a=6,b=19;
unsigned int c;
int D;
c=a-b+7;
D=b*c;
A=b+c+d;
b=-a; ' Vtable
a,2,6
b,2,19
c,2,0
d,2,0
of Vtable
' Vupdate
1,6;2,19
3,0
4,0
3,65530
4,-114
1,-101
2,101
of Vupdate
of Practice
void Main () {
Long x,y;
int a,b,c,d;
x=5;
y=6;
a=7;
b=8;
C=x+a;
D=y+b;
printf ("c=x+a=%d,d=y+b=%d\n", c,d);
}
Explains main to return void, that is, no value of any type is returned
X,y is defined as a long type
A,B,C,D is defined as type int
5->x
6->y
7->a
8->b
X+a->c
Y+b->d
Displays the results of the program's run of Long x,y;
int a,b,c,d;
C=x+a;
D=y+b;
As you can see from the program: X, Y is a long integer variable, a, B is the basic integer variable. Operations are allowed between them, and the result is a long integer. But c,d is defined as the base integral type, so the final result is the basic integer. This example shows that different types of quantities can participate in operations and assign values to each other. The type conversion is done automatically by the compilation system. Rules about type conversions will be introduced later.

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.