Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Basic data types in C language programs (characters, integers, long integers, floating points)
Basic data types in C language programs (characters, integers, long integers, floating points)
Chapter 2 Data Types, Operators and Expressions

This chapter mainly introduces various data types, operators and expressions of C language.

2. Data type of1c language

The data types of C language are classified as follows:

Basic types: There are four types: integer type, real type (also called floating point type), character type and enumeration type.

Structure type: There are three types: array type, structure type and * * * usage type.

Pointer type

Empty type

2.2 Constants and variables

2.2. 1 constant

1, the concept of constant: the amount whose value cannot be changed during the running of a program is called a constant.

2. Classification of constants:

Integer constant

Real constant

Character constant

Symbolic constant

3. The type of constant can be judged by the written form.

variable

1, the concept of variable: the amount whose value can change during the running of a program is called a variable.

2. Variable name: each variable must have a name-variable name, and variable naming follows the rules of identifier naming.

3. Variable value: During the running of the program, the variable value is stored in the memory. In a program, the value of a variable is referenced by its name.

4. Naming rules for identifiers:

It can only consist of letters, numbers and underscores, and it starts with a letter or underscore.

Effective length: varies from system to system, but at least the first 8 characters are valid. If it is too long, the extra part is discarded.

Keywords in C language cannot be used as variable names.

Note: C language is very sensitive to the size of English letters, that is, the case of the same letter is considered as two different characters.

Traditionally, English letters in variable names and function names are lowercase to increase readability.

5. Definition and initialization of variables

In C language, all variables required to be used must be defined before being used; Giving an initial value when defining a variable is also called variable initialization.

General format of (1) variable definition

Data type variable name [,variable name 2...];

For example, the radius, length and area of the float;

(2) General format of variable initialization

Data type variable name [= initial value] [,variable name 2[= initial value 2]...];

For example, floating radius =2.5, length, area;

2.3 integer data

2.3. 1 integer variable

1, classification

Integer variables are divided into four categories according to the number of bytes occupying memory:

(1) basic integer (type keyword is int).

(2) short integer (type keyword is short [int]).

(3) long integer (the type keyword is long [int]).

(4) Unsigned integer. Unsigned types are divided into unsigned basic integers (unsigned [int]), unsigned short integers and unsigned long integers, and can only be used to store unsigned integers.

2. Number of bytes occupied in memory and range

The number of bytes of memory occupied by integer variables of the above types varies from system to system. In 16-bit operating system, variables of type int are generally represented by 2 bytes, with long type (4 bytes) ≥int type (2 bytes) ≥ short type (2 bytes).

Obviously, different types of integer variables have different ranges. N-byte (signed) integer variable in memory, the value range is:-2n * 8-1~ (2n * 8-1-1); The range of unsigned integer variables is 0~(2n*8- 1).

For example, the value range of int variable in PC is-22 * 8-1~ (22 * 8-1-1), that is,-32768 ~ 32767; The value range of unsigned variables is 0~(22*8- 1), that is, 0~65535.

Chapter 2 Data Types, Operators and Expressions

This chapter mainly introduces various data types, operators and expressions of C language.

2. Data type of1c language

The data types of C language are classified as follows:

Basic types: There are four types: integer type, real type (also called floating point type), character type and enumeration type.

Structure type: There are three types: array type, structure type and * * * usage type.

Pointer type

Empty type

2.2 Constants and variables

2.2. 1 constant

1, the concept of constant: the amount whose value cannot be changed during the running of a program is called a constant.

2. Classification of constants:

Integer constant

Real constant

Character constant

Symbolic constant

3. The type of constant can be judged by the written form.

variable

1, the concept of variable: the amount whose value can change during the running of a program is called a variable.

2. Variable name: each variable must have a name-variable name, and variable naming follows the rules of identifier naming.

3. Variable value: During the running of the program, the variable value is stored in the memory. In a program, the value of a variable is referenced by its name.

4. Naming rules for identifiers:

It can only consist of letters, numbers and underscores, and it starts with a letter or underscore.

Effective length: varies from system to system, but at least the first 8 characters are valid. If it is too long, the extra part is discarded.

Keywords in C language cannot be used as variable names.

Note: C language is very sensitive to the size of English letters, that is, the case of the same letter is considered as two different characters.

Traditionally, English letters in variable names and function names are lowercase to increase readability.

5. Definition and initialization of variables

In C language, all variables required to be used must be defined before being used; Giving an initial value when defining a variable is also called variable initialization.

General format of (1) variable definition

Data type variable name [,variable name 2...];

For example, the radius, length and area of the float;

(2) General format of variable initialization

Data type variable name [= initial value] [,variable name 2[= initial value 2]...];

For example, floating radius =2.5, length, area;

2.3 integer data

2.3. 1 integer variable

1, classification

Integer variables are divided into four categories according to the number of bytes occupying memory:

(1) basic integer (type keyword is int).

(2) short integer (type keyword is short [int]).

(3) long integer (the type keyword is long [int]).

(4) Unsigned integer. Unsigned types are divided into unsigned basic integer (int), unsigned short integer and unsigned [int] integer, which can only be used to store unsigned integers.

2. Number of bytes occupied in memory and range

The number of bytes of memory occupied by integer variables of the above types varies from system to system. In 16-bit operating system, variables of type int are generally represented by 2 bytes, with long type (4 bytes) ≥int type (2 bytes) ≥ short type (2 bytes).

Obviously, different types of integer variables have different ranges. N-byte (signed) integer variable in memory, the value range is:-2n * 8-1~ (2n * 8-1-1); The range of unsigned integer variables is 0~(2n*8- 1).

For example, the value range of int variable in PC is-22 * 8-1~ (22 * 8-1-1), that is,-32768 ~ 32767; The value range of unsigned variables is 0~(22*8- 1), that is, 0~65535.

Real data

2.4. 1 real variable

There are two kinds of real variables in C language:

(1) Single precision type: the type keyword is float, which generally takes 4 bytes (32 bits) and provides 7 significant digits.

(2) double precision type: the type keyword is double, which generally takes 8 bytes and provides 15~ 16 significant digits.

Real constant

1, expression form

Real constant is a real number, also known as floating point number in C language, and its value is expressed in two forms:

(1) decimal form: for example, 3.14,9.8.

(2) Exponential form: e (e) <; Integer exponent >. Such as 3.0E+5.

2. About types

Real constants are not divided into floating-point type and double-precision type. A real constant that can be assigned to a real variable (floating point or double precision).

2.5 Character data

2.5. 1 character constant

1, the definition of character constant

A single character enclosed in a pair of single quotation marks is called a character constant.

Such as' a',' 1','+'and so on.

2. Escape characters

C language also allows the use of special forms of character constants, that is, escape characters starting with the backslash \ ".

Note: If backslashes or single quotes themselves are used as character constants, you must use escape characters:' \ \',' \'.

[Case 2. 1] Output printable and unprintable characters with escape characters.

Master ()

{

printf(" \ x4F \ x4B \ x 2 1 \ n "); /* is equivalent to printf ("OK! \ n "); */

printf(" \ x 15 \ xAB \ n ");

}

The running results of the program are as follows:

All right!

Character variable

The type keyword of a character variable is char, which generally takes up 1 byte of memory cells.

1. Storage of variable values

Character variables are used to store character constants. Storing a character constant into a character variable is actually storing the ASCII code value (unsigned integer) of a character into a memory cell.

For example,

Char ch 1, ch2/* define two character variables: ch 1, ch2*/

ch 1 = ' a '; Ch2 =' b/* Assign a value to a character variable */

2. Features

Character data is stored in memory in ASCII code of characters-an unsigned integer, and its form is the same as the storage form of integers, so C language allows universal use between character data and integer data.

(1) A character data, which can be output in the form of characters or integers.

[Case 2.2] Character output and integer output of character variables.

Master ()

{

char ch 1,ch2

ch 1 = ' a '; ch2 = ' b

printf("ch 1=%c,ch2=%c\n ",ch 1,CH2);

printf("ch 1=%d,ch2=%d\n ",ch 1,CH2);

}

Program running results:

ch 1=a,ch2=b

ch 1=97,ch2=98

(2) Arithmetic operation on character data is allowed, that is, arithmetic operation on its ASCII code value.

[Case 2.3] Arithmetic operation of character data.

Master ()

{

char ch 1,ch2

ch 1 = ' a '; ch2 = ' B

printf("ch 1=%c,ch2=%c\n ",ch 1-32,CH2+32); /* Case conversion of letters */

/* Output values greater than 256 as characters */

printf("ch 1+200=%d\n ",ch 1+200);

printf("ch 1+200=%c\n ",ch 1+200);

printf("ch 1+256=%d\n ",ch 1+256);

printf("ch 1+256=%c\n ",ch 1+256);

}

Program running results:

ch 1=A,ch2=b

ch 1+200=297

ch 1+200=)

ch 1+256=353

ch 1+256=a