Scattered knowledge points:
- variables : In the C language, each variable must first be defined and then referenced. The so-called variable existence refers to the system to allocate a piece of storage space for the variable, at this time the operation of the variable is to the variables corresponding to the storage space stored in the data to operate. The time when a variable occupies a storage space is called the lifetime of the variable, and the area that the variable can refer to is called the scope of the variable. The scope of a variable depends on where the variable is defined.
- Global Variables : variables defined outside the function are called global variables. The lifetime of a global variable is the program file that defines the variable, starting at the definition and ending at the end of the program file. If some local variables in the program file have the same name, the scope of the global variable should be subtracted from this part of the area.
- Local Variables : variables defined inside a function, including formal parameters defined in the parameter table, are called local variables. The lifetime and scope of local variables are functions that define these variables. If a block variable with the same name as a local variable appears in the function, the scope of the local variable is the area where the block variable exists, but its life cycle does not end. (variables that can have the same name between different functions)
- block variable : A variable defined in a compound statement is called a block variable. The lifetime of a block variable is only a compound statement block that defines it. Advantage: You can increase the utilization of storage space.
- static variable : A variable allocates storage space to the system when it starts running, and all of its allocated storage space is recycled until the program ends.
- Format: Static data type variable name;
- Description
- Advantages: Can improve the program's clarity, reduce the complexity of the program, improve the maintainability of the program.
- A struct's variable is called a member, (in a struct, a member can belong to any data type). The syntax format for its type declaration:
struct < struct type name > {
< data types > < members 1>;
< data types > < members 2>;
......
< data types > < members n>
};
- in the In the C language, the user is allowed an alias for the data type that already exists in the format:typedef Original data type name new data type name ;
For example: typedef int INTEGER;
Advantages: Can improve the clarity of the program, increase the readability of the program
After combining:
typedef struct POINT_TYPE {
Int x;
Int y;
} point
Point P1, p2
Equivalent to:
struct Point_type p1, p2;
Equivalent to:
struct point_type{
Int x;
Int y;
} p1, p2;
Initialization of struct type variables: struct Point_type p1 = {10,20};
Reference to struct type variable: p1.x; P1.y
Input of struct type variable: scanf ("%d%d", &p1.x,&p1.y);
Output of struct type variables: printf ("%d%d", p1.x,p1.y);
Assignment of struct type variables: p1.x=6; p1.y=8;
function Description: Enter The basic information of the students via the keyboard and output on the above. Then, enter a month and date through the keyboard to find and output the student information for the year after this given date.
code example:
1#include <stdio.h>2 #pragmaWarning (disable:4996)3 #defineNUM 204typedefstruct{5 intYear/*years*/6 intMonth/*Month*/7 intDay/*Day*/8 } DATE;9 Tentypedefstruct{ One intNum/*School Number*/ A Charname[ -];/*name*/ -DATE birthday;/*Date of birth*/ - Chardepartment[ -];/*Affiliated Faculties*/ the Charmajor[ +];/* Major in major*/ - } studentinfo; - voidInputinfo (Studentinfo s[]); - voidOutinfo (Studentinfo s[]); + voidSearchInfo (Studentinfo s[], date date); - + Main () A { at Studentinfo S[num]; - date date; - Inputinfo (s); - Outinfo (s); -printf"\ n Enter a date (month,day): \ n"); -scanf"%d%d", &date.month, &date.day); in SearchInfo (S, date); - } to + voidInputinfo (studentinfo s[]) - { the inti; *printf"Enter%d studennts information (name,birthday,department,major): \ n", NUM); $ for(i =0; i < NUM; i++){Panax NotoginsengS[i].num = i +1; -scanf"%s", s[i].name); thescanf"%d%d%d", &s[i].birthday.year, &s[i].birthday.month, &s[i].birthday.day); +scanf"%s", s[i].department); Ascanf"%s", s[i].major); thePutchar ('\ n'); + } - } $ $ voidOutinfo (studentinfo s[]) - { - inti; theprintf"\ n Num Name Birthday Department major\n"); - for(i =0; i < NUM; i++){Wuyiprintf"%4d%14s%4d/%2d/%2d%16s%16s", the S[i].num, S[i].name, S[i].birthday.year, S[i].birthday.month, S[i].birthday.day, - s[i].department, s[i].major); WuPutchar ('\ n'); - } About } $ - voidSearchInfo (Studentinfo s[], date date) - { - inti; A for(i =0; i < NUM; i++){ + if(s[i].birthday.month>date.month) { theprintf"\n%4d%16s%2d/%2d", S[i].num,s[i].name, S[i].birthday.month, - s[i].birthday.day); $ Continue; the } the if(S[i].birthday.month = = Date.month && s[i].birthday.day>date.day) { theprintf"\n%4d%16s%2d/%2d", S[i].num, S[i].name, S[i].birthday.month, the s[i].birthday.day); - } in } the}
C Language Programming Case Tutorial (2nd edition) code note (iii)