[final review] โšก Exam month is coming! C language review, this one takes you to escape from the hanging section! (end)

I'm back! Less than 4 hours after the last blog post was sent out, this blog post has started again. I hope it will be smooth and smooth. Run away from the hanging section! ๐Ÿ˜ข

If you haven't seen the link in the previous section, you can jump~ C language review (Part I) Oh, this one will follow the last one~

If you need source files, you can send me a private letter!

4. Function

A function is a module with certain functions. The so-called function name is a name for the function.

Note: functions are functions. Each function is used to implement a specific function. The name of the function should reflect the function it represents, so that the readability of the code will be greatly improved

I remember the sentence in the previous article, "a C program can be composed of a main function and several other functions." C language is a completely process oriented language. In programming, we should be good at using functions to reduce the writing of repeated code and reduce code redundancy as much as possible, which can also improve the maintainability of code and facilitate the realization of modular programming.

4.1 define a function

Through the above explanation, I believe I have deepened my understanding of functions and the role of functions.

Defining a function includes the following contents:

  1. Specify the name of the function for future function calls
  2. Specifies the type of the function, that is, the type of the return value of the function
  3. Specify the name and type of the function's parameters to pass when calling the function
  4. Write the function of the function, which is the core content of the function

4.1.1 define a parameterless function

When the function does not need to accept the data passed by the user, it does not need to take parameters

Return value type function name() {
    function
}

Let's define a function that adds 1 to 100

int sum()
{
    int i, sum = 0;
    for (i = 1; i <= 100; i++)
    {
        sum += i;
    }
    return sum;
}

Save the calculation result to sum and return it to the caller of the function through the return statement. The type of return value is int

4.1.2 function without return value

Some functions do not need a return value, but only need to execute code. We can specify the type of return value through void

void hello() }{
    printf("hello world");
}

This function has no return value. Its function is to output hello world. Type void indicates empty type or no type. Most of them mean no return statement

4.1.3 defining parametric functions

When the function needs to receive the data passed by the user, it needs to take parameters when defining

int max(int x, int y) {
	int z;
	z = x > y ? x : y;
	return z;
}

The function above is to find the larger of the two numbers. When calling the main function, pass the data to the formal parameters x and y, judge the larger number in the two books in the function body, and return the return value to the caller of the function through the return statement

be careful:

  1. The data description of the parameter can be omitted, and the default value is int type
  2. The function name needs to follow the identifier naming convention
  3. The function needs to be defined before use. If the function is written after the main function, the declaration of the function needs to be written in main before the function call
  4. Nested definition of functions is not allowed in C language

4.2 calling functions

The way to call the function is very simple. Take calling the maximum of the two numbers calculated above as an example

c = max(a, b);

In this way, we can call the function, transfer a and B to the max function, and assign the value of the return value z to c after the function is executed, so that c can get the larger value of a and B

Let's write a program to practice

Input two integers and output the larger one. Use the function to realize it

First, we write the max function to return the larger of the two numbers

int max(int x, int y) {
	int z;
	z = x > y ? x : y;
	return z;
}

Next, we write the main function

int main()
{
    int a, b, c;
    printf("Please enter two numbers\n");
    scanf("%d,%d", &a, &b);
    c = max(a, b);
    printf("max is %d", c);
    return 0;
}

Receive the data a and B input by two users in the main program, and return a large number through the function to realize the function

Note: the program executes from top to bottom. When the function name is encountered, the value is passed to the calling function. When the program gets the return value or the calling function ends, it continues to execute.

4.3 difference between formal parameter and actual parameter

In the previous section, we reviewed how to define and call functions.

If the function is a processing factory, the parameter of the function is the raw material of the factory, and the return value is the processed product.

important

  1. The formal parameter will allocate memory only when the function is called. After the call, the memory will be released immediately. Therefore, the formal parameter variable is only valid inside the function and cannot be used outside the function.

  2. Arguments and formal parameters must be strictly consistent in quantity, type and order, otherwise the error of "type mismatch" will occur. If automatic type conversion or forced type conversion is performed, the argument type can also be different from the formal parameter type.

  3. The data transfer in the function call is one-way. You can only pass the value of the argument to the formal parameter, but not the value of the formal parameter to the argument in the opposite direction. That is, changing the formal parameter will not affect the value of the argument.

Note: the change of formal parameter will not change the change of actual parameter when passing value; When the address is passed, the change of the formal parameter may change the value corresponding to the actual parameter

4.4 nested call of functions

Functions cannot be defined nested, but can be called nested, that is, calls to another function are allowed during the definition or call of one function. The content of this part is too simple to elaborate, that is, calling another function in one function, infinite dolls

Recursive function call

In the process of calling a function, the function itself is called directly or indirectly, which is called recursive call, which is also one of the characteristics of C language. Recursive function plays an important role in solving many mathematical problems, such as calculating the factorial of a number, generating Fibonacci sequence, and so on.

Note: recursion must have an end condition, otherwise it may call an infinite loop

Next, the nth term of Fibonacci sequence is output by recursion

#include <stdio.h>
int Fib(int n)
{
    if (n == 0)
    {
        return 1;
    }
    else if (n == 1)
    {
        return 1;
    }
    else
        return Fib(n - 1) + Fib(n - 2);
}
int main()
{
    int n, ret;
    printf("Please enter n:");
    scanf("%d", &n);
    ret = Fib(n);
    printf("%d\n", ret);
    return 0;
}

Important statement return Fib(n - 1) + Fib(n - 2); Call this function again through return until the end condition is reached.

4.6 global and local variables

4.6.1 local variables

The variables defined inside the function are called local variables. Its scope is limited to the inside of the function. It is invalid after leaving the function, and an error will be reported if it is used again.

int name(int a)
{
    int b, c;
    return a + b + c;
}
int main()
{
    int x, y;
    return 0;
}
  1. The variables defined in the main function are also local variables and can only be used in functions. The main function is also a function and has equal status with other functions
  2. The process of transferring values from arguments to formal parameters is also the process of assigning values to local variables
  3. You can use the same variable name in different functions. They represent different data, allocate different memory, and do not interfere with each other. (I secretly understand it as a block level scope in js)

4.6.2 global variables

Variables declared outside the function are called global variables. Its scope is the whole scope, that is, the whole file

4.6.3 exercises

Enter the length, width and height of the box to find its volume and the area of three faces. (write a function implementation)

#include <stdio.h>

int s1, s2, s3; //the measure of area

int volume(int a, int b, int c)
{
    int v; //volume
    v = a * b * c;
    s1 = a * b;
    s2 = b * c;
    s3 = a * c;
    return v;
}

int main()
{
    int v, length, width, height;
    printf("Enter length, width and height: ");
    scanf("%d %d %d", &length, &width, &height);
    v = volume(length, width, height);
    printf("v=%d, s1=%d, s2=%d, s3=%d\n", v, s1, s2, s3);
    return 0;
}

Three global variables are used to record the area of three faces. In this way, the value of the corresponding area can be obtained directly by accessing the global variables through the main function, and the volume v can be obtained by returning the value

Note: it is recommended not to use global variables when they are not necessary (this is the same in other languages)

reason:

  1. Global variables occupy the storage unit during the whole execution of the program, not only when needed
  2. It reduces the generality of functions and is not conducive to modular programming
  3. Reduces the readability of the code

4.7 internal and external functions

  1. Functions that cannot be called by other source files are called internal functions. Internal functions are defined by the static keyword, so they are also called static functions, such as static int max()
  2. Functions that can be called by other source files are called external functions, which are defined by the external keyword in the form of external int max()
  3. If the scope of function is not specified, the system will default to external function
// hello.c Documents
 void hello()    
{
   printf("hello world");   
}
//main.c Documents
#include<stdio.h>
#include "hello.c"
int main() {
    hello();
    return 0;
}

4.8 exercises

First question

In C language, the following statements about functions are correct.

A. Functions can be defined nested or called nested. B. functions can be defined nested but not called nested

C. Functions cannot be nested, but they can be nested. D. functions cannot be nested, nor can they be nested

Answer: C

Second question

If there is a function prototype as shown below, the return type of the function is ().

abc(float x,float y)

A. void B. double C. int D. float

Answer: C default int

Question 3

In C language, the error in the following description is ().

A) The variables defined in the main function are global variables

B) In the same program, global variables and local variables can have the same name

C) The scope of the global variable starts from the definition to the end of the source program file

D) The scope of a local variable is limited to the local scope it defines

Answer: function A

5. Pointer

Each variable has a memory location, and its memory address can be accessed by using the & get address character, which represents an address in memory. ๐ŸŸ

Pointer is the memory address. Pointer variable is the variable used to store the memory address. Like other variables or constants, you must declare the addresses of other variables before using pointers to store them.

Format: type * variable name

int    *ip;    /* An integer pointer */
double *dp;    /* A double pointer */
float  *fp;    /* A floating point pointer */
char   *ch;    /* A character pointer */

The only difference between pointers of different data types is that the data types of variables or constants pointed to by the pointer are different.

Figuratively speaking: a room number 1304 is hung at the door of a room. This 1304 is the address of the room, or 1304 points to the room. Therefore, the address visualization is called pointer.

Pointer variables can point to any data type, but no matter how many bytes the pointed data occupies, a pointer variable occupies four bytes.

5.1 pointer variables

The variable storing the address is a pointer variable, which is used to point to another object

Note: when defining pointer variables, you must determine the pointer type. For example, pointers to int variables need to be stored with int type pointers.

Distinguish between pointer variables and pointers. Pointer is an address, and pointer variables are variables that store addresses

5.1.1 using pointer variables

Accessing integer variables through pointer variables

#include <stdio.h>
int main()
{
    int a = 100;
    int *pointer;
    pointer = &a;
    printf("*pointer The value of is%d", *pointer);
    return 0;
}

First define an integer variable, and then define a pointer variable to point to this integer variable. By accessing the pointer variable, you can find the variable it points to, and then get the value of the variable

5.1.2 defining pointer variables

To define a pointer variable, you need to precede the variable name with an asterisk *, for example

int *try;

*Indicates that this is a pointer variable, and int indicates that the data type of the data pointed to by the pointer variable is int

int a = 100;
int *p = &a;

When the pointer variable p is defined, it is initialized at the same time, and the address of variable a is given to it. At this time, P points to a

be careful:

  1. The pointer variable p needs to receive an address, so you need to use the & get address character to get the address of a
  2. The pointer variable must be defined with an * sign, and the pointer variable cannot be assigned with an * sign
  3. The type of pointer variable p is int * instead of int. Oh, they are completely different~

5.1.3 reference pointer variable

  1. Assign values to pointer variables
p = &a; //The address of a is assigned to the pointer variable p

The value of pointer variable p is the address of variable a, and P points to a

  1. The variable pointed to by the reference pointer variable
p = &c;
printf("%d",*p);

printf("%d",*p); Output the value of the variable pointed to by the pointer variable p in the form of an integer

*p = 1;

Indicates that the integer 1 is assigned to the variable pointed to by p, that is, c = 1

  1. Reference the value of the pointer variable
printf("%o",p);

The function is to output the value of pointer variable p in octal form. If P points to a, the output is the address of A

*And &: the functions of these two symbols are relative. It can be understood that & is the memory address of the variable, and the * sign is the value of the variable on the address, that is, the function of de indexing

int a;
*&a = a;

*&A can be understood as * (& A), & A represents the address of variable a, and * (& A) represents the data on this address

5.2 pointer variables as function parameters

In the previous function section, we talked about

"Pass the value, the change of the formal parameter will not change the change of the argument; pass the address, the change of the formal parameter may change the value corresponding to the argument"

Pointer variables as function parameters are the case of address passing, which can help us solve some problems. A typical example is to exchange the values of two variables

When we want to exchange two variables, we can declare a swap function to exchange the values of two variables, but the conventional value transfer method is not feasible

#include <stdio.h>
void swap(int a, int b){
    int temp;  
    temp = a;
    a = b;
    b = temp;
}
int main(){
    int a = 1, b = 2;
    swap(a, b);
    printf("a = %d, b = %d", a, b);
    return 0;
}

Because a and b inside the function are local variables, they occupy different memory. Changing the values of a and b in the swap function will not affect the values of a and b in the main function

This problem can be solved by using pointer variables as function parameters, because the transfer of parameters is the memory address. External functions directly complete the operation by modifying the value on the memory address

#include <stdio.h>
void swap(int *p1, int *p2){
    int temp; 
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}
int main(){
    int a = 1, b = 2;
    swap(&a, &b);
    printf("a = %d, b = %d", a, b);
    return 0;
}

5.3 reference pointer through array

5.3.1 pointers to array elements

๐ŸŽ‰ The pointer to an array element is the address of the array element

You can use a pointer variable to point to an array element, such as

int a[3] = {1, 2, 3};
int *p;
p = &a[0]

The subscript method a[1] or pointer method can be used to reference array elements. The pointer method occupies less memory and runs faster

In the array part, we know that the array name represents the address of the first element, so we can directly write p = a to point to the first element of the array

5.3.2 operation when referencing array elements

When the pointer has pointed to an array element, the following operations can be performed

p + 1: points to the next element in the same array

p - 1: points to the previous element of the same array

Note: p + 1 is not simply adding one to the value, but adding the number of bytes occupied by an array element. For example, if an element of a float type array occupies 4 bytes, the value of p will add 4, which points to the next element

If the initial value of p points to the first element a[0] of the array, it is conceivable that p + i can access the address of the array element a[i]. For example, the value of p + 9 is & A [9]. Similarly, when we get the address of the array element, we can use the * sign to get its value, such as * (p + i) = a[i]

5.3.3 exercises

๐ŸŽ‰ Output 10 elements of integer array a through pointer variable

#include <stdio.h>
int main()
{
    int *p, i, a[10];
    p = a; //a's address
    printf("Please enter 10 integers:\n");
    for (i = 0; i < 10; i++)
    {
        scanf("%d", p++);
    }
    p = a;//Since the pointer p changes during traversal, it needs to be re assigned
    for (i = 0; i < 10; i++, p++)
    {
        printf("%d ", *p);
    }
    return 0;
}

When traversing, p + + is used. After each traversal, the pointer p points to the next bit of the array

5.4 pointer array and array pointer

Pointer array: it is an array. The elements of the array are pointers. The number of bytes occupied by the array is determined by the array itself. Also called an array of pointers

Array pointer: it is a pointer that points to an array. In a 32-bit system, it always occupies 4 bytes, which is also called a pointer to an array

5.5 reference string through pointer

We also mentioned earlier that "there is no string variable in c language", but strings can be stored through character arrays and character pointers

Let's start with a simple topic

Output a string through a character pointer variable

#include <stdio.h>
int main()
{
    //Defines a character pointer to store a string
    char *string = "i am ljc";
    // output
    printf("%s", string);
    return 0;
}

In the above code, i am ljc is successfully output. We can know that when outputting string, the character pointer first refers to the first character of the string, and because the string occupies continuous memory space in memory, under the output control character% s, the system will automatically output the first character of the string, and then make the character pointer point to the next character, Until \ 0 is encountered.

Comparison between character pointer variable and character array

  1. The character array is composed of several elements, one character is placed in each element, and the address is stored in the character pointer variable
  2. The assignment methods are different. You can assign values to character pointer variables instead of array names
  3. The storage units are different. During compilation, the character array allocates several storage units to store the value of each element, while the character pointer variable only occupies 4 bytes (different compilers may be different)
  4. The value of pointer variable can be changed, while the character array name represents a fixed value and cannot be changed

5.6 pointer as function return value

When the return value of a function is a pointer, this function is called a pointer function

#include <stdio.h>
#include <string.h>
char *longStr(char *str1, char *str2){
    if(strlen(str1) >= strlen(str2)){
        return str1;
    }else{
        return str2;
    }
}

The above code defines a function that returns a long string

When using a pointer function, you should note that all local data defined inside the function will be destroyed after the function is run. The pointer returned by the function should not point to these data

5.7 exercises

๐ŸŽ‹ First question

Defined: int x,*p;, The statement that can make the pointer variable p point to the variable x is ().

A)ใ€€*p=&x;ใ€€ใ€€B)ใ€€p=&x; C)ใ€€*p=x;ใ€€ใ€€D)ใ€€p=*&x;

Answer: B

๐ŸŽ„ Second question

If there is a statement int * P, a = 10; p=&a; The following set of options representing the address is ().

A๏ผŽ a, p, *&a B๏ผŽ &*a, &a, *p

C๏ผŽ*&p, *p,&a D๏ผŽ&a, &*p, p

Answer: D

๐ŸŽ Question 3

If char s[10] is defined; The address that does not represent s[1] in the following expression is ().

A๏ผŽ s+1 B๏ผŽ s++ C๏ผŽ &s[0]+1 D๏ผŽ &s[1]

Answer: B

๐Ÿงถ Question 4

If definition: int a = 511, * b = & A; Then printf("%d\n", *b); The output result of is:

a. Address without definite value B.a C.512 D.511

Answer: D

๐Ÿ”‹ Question 5

Several are output in the following program*

A๏ผŽ 9 B๏ผŽ 5 C๏ผŽ 6 D๏ผŽ7

#include <stdio.h>
int main()
{
    char *s = "\ta\018bc";
    for (; *s != '\0'; s++)
    {
        printf("*");
    }
}

Answer: C C \ 0 is an escape character, which means that it is followed by an octal number

definitionmeaning
int *p;p can point to data of type int
int **p;p is a secondary pointer to data of type int *
int *p[n];p is an array of pointers. [] has higher priority than *, so it should be understood as int *(p[n]);
int (*p)[n];p is a two-dimensional array pointer
int *p();p is a function whose return value type is int*
int (*p)();p is a function pointer to a function whose prototype is int function()

6. Structure

Structure is essentially a user-defined data type, but this data type is more complex. It is composed of many basic types such as int, char, float and so on

From the perspective of front-end js, I will image the structure as an object in js

In this part, I didn't write the content of the linked list. In my previous blog, I wrote the complete operation idea of realizing the linked list with js. In fact, the ideas are the same, but the syntax is different, so I don't elaborate more. If you are interested, you can see the previous blog: One article takes you to the front-end necessary data structure - linked list!!

6.1 definition and use of structures

6.1.1 create structure type

struct can be used to store a set of different data types

The following structure is used to store the personal information of a member

struct people
{
    char *name;  //full name
    int num;     //Student number
    int age;     //Age
    float score; //achievement
};

people is the name of the structure, which contains four members. Structure members are defined in the same way as variables and arrays, but cannot be initialized.

Special note: the curly brackets behind the structure need to be marked with semicolons

6.1.2 defining structural variables

In the above, we defined a structure type, which we can use to define variables

struct people s1, s2;

In this way, we define two variables s1 and s2. Their types are people and they are composed of four members. Figuratively speaking: the defined people is equivalent to a template, and the variables of this type will have its characteristics

struct people
{
    char *name;  //full name
    int num;     //Student number
    int age;     //Age
    float score; //achievement
} s1, s2;

You can also define variables directly at the end of the structure

Each member of the structure is stored continuously in memory, which is similar to the array. However, due to the complex data types in the structure and the gap between each member, there is a problem of structure memory alignment!

6.1.3 reading and writing values of structure members

Use a point number Get a single member, or assign a value to the member Number is called member access operator!

You can breathe a sigh of relief now. This is so similar to the object. In fact, learning a programming language will be easy to learn other languages when you learn its ideas, so you must first learn to step over the threshold ~ rush

In this way, you can get the value of the member or assign a value

#include <stdio.h>
int main()
{
    struct people
    {
        char *name;  //full name
        int num;     //Student number
        int age;     //Age
        float score; //achievement
    };
    struct people my;
    my.name = "LJC";
    my.num = 1023;
    my.age = 19;
    my.score = 100;
    printf("full name:%s,Age:%d,Student number:%d,fraction:%.1f", my.name, my.age, my.num, my.score);
    return 0;
}

Running result: Name: LJC, age: 19, student number: 1023, score: 100.0

In this way, we realize the assignment of structure variables.

๐ŸŽƒ Structure is a user-defined data type. It is a template for creating variables and does not occupy memory space. The memory space will be occupied only when the structure variable is created!!!

6.2 structure array

Each element in the structure array index group is a structure, which is very convenient. We can put the students of a class in a structure array, so that the students of a class are bound to the members of the structure

struct stu{
    char *name;  //full name
    int num;  //Student number
    int age;  //Age
    int score;  //achievement
}class[45];

A class of 45 students is defined

The way of reading and writing is the same as that of structure variables. Initialization can be carried out while the structure array is defined, or the length of the array can not be given, such as:

struct stu{
    char *name;  //full name
    int num;  //Student number
    int age;  //Age
    int score;  //achievement
}class[] = {
    {"ljc",3213,19,100},
    {"ljc",3223,19,99}
};

The use of structure array is also very convenient. You can access and assign values

class[0].score = 99;

6.3 structure pointer

The structure pointer is the pointer to the structure, and the starting address of a structure variable is the pointer to the structure variable.

The general form of defining structure pointer is struct structure name * variable name

struct stu{
    char *name;  //full name
    int num;  //Student number
    int age;  //Age
    int score;  //achievement
} my = {"ljc", 1023, 19, 100};// Defines a structure variable my
struct stu *pmy = &my; // Define the structure pointer pmy, give the address of my and let it point to my

You can also define the structure pointer at the same time as defining the structure: (see several forms more, and the exam will not be unfamiliar ~)

struct stu{
    char *name;  //full name
    int num;  //Student number
    int age;  //Age
    int score;  //achievement
} my = {"ljc", 1023, 19, 100}, *pmy = &my;

๐ŸŽจ Special note: the variable name of the structure is different from the array name. The array name will be converted into the array pointer pointing to the first element of the array in the expression, and the variable name of the structure will not need to carry the & address character~

6.3.1 obtaining structure members

There are two methods to obtain, one is to use the member access operator, the other is to use the - > operator, which is called arrow. You can directly obtain the structure member pmy - > num through the structure pointer

Example: output the value of structure member through structure pointer

#include <stdio.h>
int main()
{
    struct people
    {
        char *name;  //full name
        int num;     //Student number
        int age;     //Age
        float score; //achievement
    } my = {"LJC", 1023, 19, 100}, *pmy = &my;
    printf("full name:%s,Age:%d,Student number:%d,fraction:%.1f", (*pmy).name, (*pmy).num, pmy->age, pmy->score);
    return 0;
}

Special note: when using the first method, there must be parentheses, because The priority of is higher than *, if you remove it, the meaning will be different

6.3.2 structure array pointer usage

The use of structure array pointer is basically the same as that of structure pointer. The defined structure pointer ps is a pointer variable pointing to the struct stu structure data type. The pointer is given an initial value in the for loop and points to the first element of the array. After the output, the pointer points to the next bit of the group, that is, ps++

#include <stdio.h>

struct stu
{
    char *name;  //full name
    int num;     //Student number
    int age;     //Age
    float score; //achievement
} student[] = {
    {"LJC", 1023, 19, 100},
    {"LIN", 1021, 19, 110},
    {"JC", 1020, 19, 120},
},
  *ps;//Define structure array pointer

int main()
{
    //Find the length of the array
    int len = sizeof(student) / sizeof(struct stu);
    printf("Name\tNum\tAge\tScore\t\n");
    for (ps = student; ps < student + len; ps++)
    {
        printf("%s\t%d\t%d\t%.1f\n", ps->name, ps->num, ps->age, ps->score);
    }
    return 0;
}

Output results

6.3.3 structure pointer as function parameter

Review with examples: calculate the average score of students' grades

Note: by passing in the structure pointer, the memory occupation can be reduced. When passing in the structure array, all the contents of the memory unit occupied by it will be passed to the formal parameters as parameters. In the process of function call, the formal parameters also need to occupy memory, so the memory overhead will be very large

#include <stdio.h>
struct stu
{
    char *name;  //full name
    int num;     //Student number
    int age;     //Age
    float score; //achievement
} student[] = {
    {"LJC", 1023, 19, 100},
    {"LIN", 1021, 19, 110},
    {"JC", 1020, 19, 120},
};

void average(struct stu *ps, int len)
{
    int i;
    float average, sum = 0;
    for (i = 0; i < len; i++)
    {
        sum += (ps + i)->score;
    }
    printf("sum = %.1f\n average=%.2f", sum, sum / len);
}
int main()
{
    //Find array length
    int len = sizeof(student) / sizeof(struct stu);
    average(student, len);
    return 0;
}

Output results

6.4 common body type

The common body is a special data type. Different from the structure, it allows different data types to be stored in the same memory location. Therefore, we can analyze it from the name. They share the same memory space

6.4.1 definition and use of common body type

The definition method and use method are basically the same as that of the structure. The definition structure adopts struct, and the definition common body adopts union keyword. The definition format is:

union Common body name {
    Member list
};

See if it's basically the same. I believe you should forget it~

The difference between a structure and a common body is that each member of the structure will occupy different memory and have no influence on each other; All members of the community occupy the same memory, which will affect each other.

The memory size of the community depends on the memory occupying the largest memory in the member. For example, define the following community:

union data{
    int a;
    char b;
    double c;
};

The biggest memory occupation is that double occupies 8 bytes, so the number of bytes occupied by the common body is 8. The arrangement of the common body in memory is shown in the figure:

It's a little ugly

Let's use an example to demonstrate the distribution of community members in memory

#include<stdio.h>
//Define Commons
union data {
    int i;
    char ch;
};
int main() {
    union data a; //Define common body variables
    printf("%d,%d\n",sizeof(a),sizeof(union data)); //Output 8 
    a.i = 0x62;
    printf("%X,%c\n",a.i,a.ch);
    a.ch = '9';
    printf("%X,%c\n",a.i,a.ch);
}

Output results

In line 10 of the code, we assign 0x62 to the common body member i, which will be stored in the first byte of memory. When ch is assigned '9', the ascII code of character 9 is 57, and the corresponding hexadecimal is 0x39. Therefore, the first byte will be rewritten to 39, so the value of member i will also be changed

6.5 enumeration types

In advanced programming, it is explained as follows: enumeration refers to the enumeration of possible references one by one, and the value of variables is limited to the range of listed values

Definition of enumeration type

The declared enumeration type starts with enum, and the format is as follows:

enum typeName {valueName1,valueName2,...}

Example: list a week

enum Weekdday {sun, mon, tue, wed, thu, fri, sat};

In this way, we declare an enumeration type enum Weekday

In this way, we can define the enum Weekday workday variable of this enumeration type, which defines an enumeration variable,

Note: unlike other variables, enumeration variables are limited to one of the specified values in curly braces

Each enumeration element represents an integer. The default is 0,1,2,3, In the above definition, sun = 0 and mon = 1 go down in sequence. We can also manually set the value of each enumeration element

enum Weekday {sun = 7,mon = 1,tue, wed, thu, fri, sat} workday;

If the value of sun is specified as 7, mon is 1, and is incremented successively, sat is 6

Several precautions:

  1. The scope of identifiers in the enumeration list is global, and the same variable name cannot be defined
  2. Enumeration elements are also called enumeration constants and cannot be assigned values. For example, sun = 7 is illegal and can only be set when defining enumeration types
  3. Enum elements are no longer variables, and you cannot use & to get their addresses

6.6 typeof declaration of new type name

typeof keyword, which can be used to give a new name to the type. For example, I can name the int type A, and then when I want to define a new int type, I can use a to declare it

typeof int a;
a num;

Of course, we won't use it in practice, because such a new name can't be understood by others. Assuming that a variable needs to be counted in a program, we can do this:

typeof int Count; // Specify Count for int
Count i; // Define variable i with count

In this way, you can clearly know that it is used for counting

Note: the new type name declared with typeof is only an alias, and the original type name can still be used normally

typeof is very useful for dealing with complex types, such as structures and common bodies

typeof struct stu
{
    char *name;  //full name
    int num;     //Student number
    int age;     //Age
    float score; //achievement
} Student;

For example, the code above defines a stu structure and uses typeof to take the alias student. Therefore, when we need to define a stu structure variable, we only need to

Student ljc;

In this way, we define a structural variable, and the semantics of the above code is very clear. Students ljc, the code will be more convenient to read and write

Let's look at more to get familiar with the following

Name a new type name instead of an array

typeof int Num[100];
Num a; //Defines an integer array a, which is equivalent to int a[100]

Replace pointer type

typeof char *String;
String p,s[10];// p is the character pointer variable and s is the character pointer array

Instead of pointer type to function

typeof int (*Pointer)();
Pointer p1,p2; //P1 and P2 are Pointer variables of Pointer type

Summary: change the variable name into a new type name according to the way of defining the variable, and add typeof at the beginning to declare that the new type name represents the original type

Step by step

// 1. Write in the way of defining variables 
int a[100];
//2. Replace the variable name with the new type name
int Num[100];
//3. Add typeof before
typeof int Num[100];
//over

6.7 exercises

๐ŸŽ€ First question

#include <stdio.h>
int main()
{
    struct sk
    {
        int a;
        float b;
    } data, *p;
    p = &data;
}

The correct reference of the above code block to member a in data is:

A)(*p).data.a B) (*p).a C)p->data.a D) p.data.a

Answer: B

๐Ÿฉฑ Second question

#include<stdio.h>
struct name1{
	char str;
	short x;
	int num;
}A;
int  main()
{
    int size =  sizeof(A);
    printf("%d\n",size);
    return 0;
}

What is the output result of the above code____

Answer: 8 test sites: structure memory alignment

๐Ÿšฉ Question 3

It's different from the previous question. Change the position of line 5 and line 6

#include <stdio.h>
struct name1
{
    char str;
    int num;
    short x;
} A;
int main()
{
    int size = sizeof(A);
    printf("%d\n", size);
    return 0;
}

How much output____

Answer: 12

7. Input and output of documents

Don't elaborate too much on the document, and write relevant operations directly

7.1 opening and closing files

7.1.1 open file

Fopen is used to open the file. The calling method is: fopen (file name, using file mode);

For example, fopen ("a1", "R") indicates that the read in method is used to open the file a1.

Note: fopen() function will obtain FILE information, including FILE name, FILE status, current read / write location, etc., save these information into a FILE structure variable, and then return the address of the variable.

FILE is < stdio h> A structure in the header FILE, so it can be used directly

FILE *fp = fopen("demo.txt", "r");

Judge whether it is opened successfully

If the opening fails, a null pointer will be returned, so you can judge whether the file is opened successfully by the returned pointer

#include <stdio.h>
int main()
{
    FILE *fp = fopen("demo.txt", "r");
    if (fp == NULL)
    {
        printf("Open failed");
    }
    else
    {
        printf("Open successfully");
    }
}

How to open fopen function

Characters that control read and write permissions

Open modeexplain
"r"**Open the file as read-only** If the file does not exist, an error occurs.
"w"**Open the file in write mode** If the file does not exist, a new file is created
"a"**Open the file as append** If the file does not exist, an error occurs.
"r+"Open the file in read-write mode. If the file does not exist, an error occurs.
"w+"Open the file in write / update mode. If the file does not exist, create a new file.
"a+"Open the file as "append / update". If the file does not exist, an error occurs.

Characters that control the reading and writing mode

Open modeexplain
"t"Text file. If it is not written, it defaults to "t".
"b"Binary file.

The read-write permission and the read-write method can be used in combination. The read-write method should be placed in the middle or tail of the read-write permission, for example:

  • Put the read-write mode at the end of the read-write permission: "rb", "wt", "ab", "r+b", "w+t", "a+t"
  • Put the read-write mode in the middle of the read-write permission: "rb +", "wt +", "ab +"

From the above characters, we can know that the file opening method consists of six characters, which mean:

  • r: Read
  • w: Write
  • a: Add
  • t: Text file
  • b: Binary file
  • +: reading and writing

7.1.2 closing documents

After using the file, you need to use the fclose function to close the file. The usage is fclose (file pointer)

#include <stdio.h>
int main()
{
    FILE *fp = fopen("demo.txt", "r");
    if (fp == NULL)
    {
        printf("Open failed");
    }
    else
    {
        printf("Open successfully");
    }
    // ...   Operation file
    // Close file
    fclose(fp);
    return 0;
}

7.2 character reading and writing files

Two functions fgetc and fputc are mainly used in character reading and writing files. The usage is very simple and can be used as an api

7.2.1 fgetc reading file

The following code is used to read the demo in the current directory Txt file

#include <stdio.h>
int main()
{
    FILE *fp = fopen("demo.txt", "rt");
    char ch;
    //If the file does not exist, prompt and exit
    if (fp == NULL)
    {
        printf("Open failed");
    }
    //Read one byte at a time until the reading is complete
    while ((ch = fgetc(fp)) != EOF)
    {
        putchar(ch);
    }
    putchar('\n'); //Output newline character
    fclose(fp);
    return 0;
}

Output results

This is exactly what I'm doing in demo Txt file, read successfully

Key point: the key to the implementation of the above code is the condition of the while loop (CH = fgetc (FP))= EOF, fgetc reads a character from the position of the position pointer every time, saves it to the variable, and then the pointer moves back one bit. When it reaches the end of the file, it cannot read the character, so EOF, over is returned

Here, you can also use the feof function to determine whether the end of the file has been reached

7.2.2 fputc write file

Writing the file is realized by fputc. The calling method fputc(ch,fp) writes the character ch to the file pointed to by the file pointer variable fp

#include <stdio.h>
int main()
{
    FILE *fp;
    char ch;
    //Judge whether the file is successfully opened
    if ((fp = fopen("demo.txt", "wt+")) == NULL)
    {
        puts("Open failed");
    }
    printf("input:\n");
    //Read one character at a time from the input and write to the file
    while ((ch = getchar()) != '\n')
    {
        fputc(ch, fp);//Output to file
    }
    fclose(fp);
    return 0;
}

7.3 string reading and writing files

In the above character reading file mode, you can only read one character at a time and write one at a time. It is really super slow, so we often don't use the character reading mode

7.3.1 fgets reading files

The fgets() function is used to read a string from the specified file and save it to the character array. Use the following method:

char *fgets (char *str, int n, FILE *fp);

str is the character array, n is the number of characters to be read, and fp is the file Pointer.

Note: since \ 0 will be automatically added to the end of the string, if you want to read 100 characters, n should take 101

Note: if a line break occurs before n-1 characters, or at the end of the file, the reading ends, so only one line of data can be read

Example: reading files using fgets

#include <stdio.h>
#include <stdlib.h>
#define N 100 / / macro definition N = 100
int main()
{
    FILE *fp = fopen("demo.txt", "rt");
    char str[N + 1];
    if (fp == NULL)
    {
        printf("Open failed");
    }
    while (fgets(str, N, fp) != NULL)
    {
        printf("%s", str);
    }
    fclose(fp);
    return 0;
}

Output results

Note: when fgets encounters line breaks, it will also be read together, so the format is the same as that of the original file, and gets will ignore the line breaks

7.3.2 fputs write file

Usage: int fputs(char *str,FILE *fp)

Example: to demo Txt file

#include<stdio.h>
#include<string.h>
int main() {
    FILE *fp = fopen("demo.txt","at+");
    char attend[100];
    char str[102] = {0};
    if(fp == NULL) {
        printf("Failed to open");
    }
    printf("Enter a sentence\n");
    gets(attend);
    strcat(str,"\n");
    strcat(str,attend);
    fputs(str,fp);
    fclose(fp);
    return 0;
}

Input and output results


There are so many ways to read and write files. There are still several ways not to write them. You can find out for yourself

epilogue

All the content of C language review is here. The blogger has finished sorting it out in a few days. If there are any problems or errors in the content, please leave a message and point out that you should study hard and make progress every day. I hope you can achieve good results here!

Keywords: C Programming pointer

Added by BlackenedSky on Thu, 03 Feb 2022 03:38:21 +0200