1. Variables
1.1 integer type
1.1.1 introduction
The integer type in C language is used to store integers. For example, int a =10
1.1.2 classification of integer types
Type name | size | Value range |
char | 1byte | -2 ^ 7 to 2 ^ 7-1 |
short | 2byte | -2 ^ 15 to 2 ^ 15-1 |
int | 4byte | -2 ^ 31 to 2 ^ 31-1 |
long | 4byte | -2 ^ 31 to 2 ^ 31-1 |
- The shaping types in C language are divided into signed and unsigned, of which the default is signed, and the above table is signed
- Integer in C program is often declared as int
1.2 floating point type
1.2.1 introduction
Floating point type in C language is used to store decimals. For example, float a = 7.3
1.2.2 classification of floating point types
type | Storage size | Range | accuracy |
float | 4byte | 1.2e-38 to 3.4E+38 | 6 decimal places |
double | 8byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
- Floating point number = sign bit + exponential bit + trailing digit (floating point number can be understood as approximate value)
- The mantissa may be lost, resulting in loss of accuracy
- The float variable needs to be followed by f
- %f default 6 decimal% X save x decimal places
-
#include<stdio.h> void main() { float a1 = 7.32f;//The float variable needs to be followed by f double a2 = 6.23213213211;//%f default 6 decimal% X save x decimal places double a3 = .97;//Equivalent to 0.97 double a4 = 9.7e2;//Equivalent to 9.7 * 10 ^ 2 printf("a1=%f a2=%f a2=%.15f a3=%f a4=%f", a1, a2, a2, a3, a4); }
1.3 character type
1.3.1 introduction
The character type is char and the size is 1byte. It can store letters or numbers.
1.3.2 code
#include<stdio.h> void main() { char c1 = 'a';//Character constants need to be enclosed in two single quotation marks char c2 = 'b'; char c3 = 97;//If char is assigned an integer, it will output its corresponding ASCII letters or symbols printf(" c1 = %c c2 = %c c3 = %c", c1, c2, c3); int c4 = c2 + 40;//char type can be used for operation printf(" c4 = %d", c4); char c5 = 'A'; printf(" c5 = %c", c5); }
- Character constants need to be enclosed in two single quotation marks
- If char is assigned an integer, it will output its corresponding ASCII letters or symbols
- char type can be used for operation
- Storage: character 'a' → 'a'ASCII code value (97) → (97) corresponding binary number (1100001) → storage
- Read: binary number (1100001) → (1100001) corresponds to decimal number (97) → (97) corresponds to ASCII code 'a' → read
1.4 boolean type
- In C89 standard, Boolean variables need to be defined by macro definition, and non-0 in bool variables is true and 0 is false
#include<stdio.h> #define BOOL int #define TURE 1 #define FALSE 0 void main() { //BOOL isOK = TURE; BOOL isOK = FALSE; if (isOK) { printf("true"); }else { printf("false"); } }
- In C99, bool type is provided, but different from C89, only 0 is false and 1 is true (non-0 is true in C89), but the header file stdpool needs to be imported h
#include<stdio.h> #include<stdbool.h> void main() { bool c1 = false; if (c1) { printf("true"); } else { printf("false"); } }
- bool is usually used for conditional control statement if and loop control statement while
1.5 basic data type conversion
1.5.1 introduction
When c program assigns and calculates variables, it will automatically convert variables with low precision into variables with high precision, that is, basic data type conversion.
1.5.2 the data types are arranged according to the accuracy
short→int→long→float→double→long double
Where unsigned int is greater than signed int
1.5.3 data type conversion rules
char,short→int→unsigned→long→double
float→double
1.5.4 code
#include<stdio.h> void main() { char a1 = 'a'; int a2 = a1; double a3 = a2; printf("a1=%c, a2=%d, a3=%lf\n", a1, a2, a3);//char,short→int→unsigned→long→double //float→double char c1 = 97; int c2 = 100; int c3 = c1 + c2; printf("c3=%d\n", c3);//Two variables with different precision are calculated, and the variable with lower precision will be automatically converted into the variable with higher precision double b1 = 97.232141221564; float b2 = b1; printf("b2 = %.10f\n", b2);//If a variable with higher precision is assigned to a variable with lower precision, the precision will be reduced }
- char,short→int→unsigned→long→double
float→double - Two variables with different precision are calculated, and the variable with lower precision will be automatically converted into the variable with higher precision
- If a variable with higher precision is assigned to a variable with lower precision, the precision will be reduced
1.6 cast type
1.6.1 introduction
Different from basic type conversion, mandatory type conversion changes from high-precision to low-precision variables. You need to add a cast character () when using. It should be noted that cast can sometimes cause precision degradation or overflow.
Cast: (type name) expression
1.6.2 code
#include<stdio.h> void main() { double a1 = 9.7;//Without changing the type of a1, a1 is still of double type int a2 = a1;//If (int) is not added, warning will occur int a3 = (int)a1;//If you add (int), there will be no warning, and the part after the decimal will be truncated directly instead of rounding printf("a1 =%f a2 = %d a3 = %d\n", a1, a2, a3); }
#include<stdio.h> void main() { //Cast is only valid for the most recent operand. To cast the entire operand, you need to put the operand in parentheses int b1 = (int)9.7 * 10 + 6 *1.5;//9 * 10 + 6 * 1.5 = 99 int b2 = (int)(9.7 * 10 + 6 *1.5);//9.7 *10 + 6 * 1.5 = 106.0→106 //After the whole calculation is bracketed, warning will not be prompted printf("b1 = %d b2 = %d\n", b1, b2); }
#include<stdio.h> void main() { char c1 = 'a'; int c2 = 1; float c3 = 1.5f; double c4 = c1 + c2 + c3;//It can run without warning, float → double char c5 = c1 + c2 + c3;//warning float→char printf("c4 = %.15f c5 = %c", c4 ,c5); }
- The forced data conversion is from large precision to small precision, and the forced data conversion does not change the type of the original operand. If the cast character (type name) expression is not used, warning will appear during the conversion from high precision to low precision. If it is used, it will not appear.
- In the process of forced conversion, the number after small precision is directly truncated rather than rounded.
- Cast is only valid for the most recent operand. To cast the entire operand, you need to put the operand in parentheses.
1.7 introduction to pointer
1.7.1 code
#include<stdio.h> void main() { int a1 = 97; //What is the address of a //1. The format of the address of the output variable is% p //2. & p means to fetch the address corresponding to variable p printf("a1 The value of is=%d a1 Your address is=%p\n", a1, &a1); //Variable → variable address → variable content //Define a pointer variable //1.int * refers to a pointer type variable //2. The name of this pointer type variable is PTR. PTR is a variable of type int * //3.ptr refers to the address of a variable of type int //4. Take out ptr's own address, & ptr //5. Take out the address of a1 stored in ptr, ptr //6. Take out the content of the address pointed to by ptr, * ptr int *ptr = &a1; printf("ptr Your address is%p ptr The stored value is an address of%p ptr The content of the address pointed to is%d", &ptr, ptr, *ptr); }
- Variable → variable address → variable content
- The format of the address of the output variable is% p
- &p means to fetch the address corresponding to the variable p
- The form of pointer is data type *, and int * refers to a pointer type variable
- The name of this pointer type variable is PTR. PTR is a variable of type int *
- ptr points to the address of a variable of type int
- Take out ptr's own address and & ptr·
- Take out the address of a1 stored in ptr, ptr
- Take out the contents of the address pointed to by ptr, * ptr
- The type of the pointer needs to be consistent with the type of the variable it points to
1.7.2 pointer practice
Write a program to obtain the address of an int variable num, display it to the interrupt, assign the address of num to the pointer ptr, and modify the value of num through ptr
#include<stdio.h> void main() { int num = 97; int* ptr = # printf("int Variable of type num Your address is%p\n", ptr); *ptr = 7; printf("Modified num The value of is%d", num); }
1.8 value transfer and address transfer (pointer transfer)
1.8.1 types of value transfer and address transfer
- Types of default transfer values: basic data type (integer type, decimal type, character type), structure and common body
- The default delivery address is similar to: pointer, array
1.8.2 value transfer
Value passing is to copy the contents (variables) of one address space to another address space.
#include<stdio.h> void main() { int a1 = 97; int a2 = a1; printf("a2 = %d", a2); }
1.8.3 pointer transfer
Pointer passing is to store the address of one address space in another address space
#include<stdio.h> void main() { int a = 97; int* b = &a;//Pointer b points to the address space of a int* c = b;//The content of pointer c is the content of pointer b, that is, both pointer b and pointer c point to the address space of a printf("a The content is%d\tb The content is%p\tc The content is%p\n", a, b, c); *c = 7;//If you change the content of the address space pointed to by pointer c, that is, the content of a, a itself will also change printf("a The content is%d\tb The content is%p\tc The content is%p\n", a, b, c); }
For example: value transfer means that you and I have 100 yuan. How much you spend has nothing to do with me; Pointer passing is that I share 100 yuan with you. You or I will change the 100 yuan.