1. Global variables and local variables
Local variables:
Concept: defining function Internal variables
Definition Format: Variable type variable name;
Scope: Start from the definition of the line to the end of the block of code
Life cycle: From the time the code runs to the end of the definition, to where the code is located
Features: The same code block can not have the same name of the variable
You can have variables of the same name in different code blocks. Internal variables override externally defined variables
Global variables:
Concept: Defining variables outside the function
Definition: Variable type variable name = value;
Declaration: Variable type variable name;
Features: 1, can not be repeatedly defined, but can repeatedly declare
2. Local variables can have the same name as global variables. Within the scope of the local variable. Then the local variable overrides the global variable
3, assuming that only declarations are undefined, the system voluntarily defines it and initializes it to 0
Scope: Start with the line defined. Until the end of the file
Life cycle: Starts from the beginning of the program. Until the program exits.
#include <stdio.h> int num;//Declaration int main (int argc, const char * argv[]) { printf ("num =%d\n", num); int num =; printf ("num =%d\n", num); { num = +; int num = ten; printf ("num =%d\n", num); num =; } printf ("num =%d\n", num); return 0;}
2. Structural body
Constructed type: Consists of an existing data type
1. Arrays: A type that consists of multiple data of the same type
Features: Only one type of number can be stored
2. Structure: Never store a set of data that represents a particular meaning
It is the data encapsulation
function-to-function encapsulation
Advantages: Improved readability of code
Improve data ease of use
Improve Code maintainability
Define the structure body:
1. Define the structure type
struct struct type name {
Member type member name;
...
};//must be added with a semicolon
2. Define structure variables by struct type
struct struct type name struct body variable name;
#include <stdio.h>int main (int argc, const char * argv[]) { // int scores[] = {59,60}; int Age name char * sex char height double weight double / int ages; char sex; double height; double weight; Char *name; // int ages[50]; Char sex[50]; // .... defines a human struct type struct person{int age ; char sex; Double height; Double weight; char *name; }; int num; int * pointer; define struct-body variables struct person person ; Visit the struct member person.age = ten; Person.sex = ' M '; Person.weight = 20.2; Person.height = 1.2; Person.name = "Xiao Ming"; printf ("age =%d,sex =%c,weight =%.2lf,height =%.2lf,name =%s\n", Person.age,person.sex,person.weight,person.height, Person.name); return 0;}
3. Define multiple types of structures
#include <stdio.h>/* first Way 1, define struct type 2, define structure variable */void test () {struct person{int age; Double height; }; struct person p1;} /* Another way to define struct types is to define struct variables at the same time */void test1 () {struct person{int age; Double height; } P1,P2 = {20,1.75}; P1 = (struct person) {10,1.2}; printf ("age =%d,height =%.2lf\n", p1.age,p1.height); printf ("age =%d,height =%.2lf\n", p2.age,p2.height); struct type definition is not enough duplicate name//struct person{//int age; Double height; } P3,P4 = {20,1.75};} /* The Third way to define an anonymous struct type is to define struct-body variables at the same time */int main (int argc, const char * argv[]) {struct{int age; char sex; } P1,P2 = {, ' M '}; P1.age = 10; P1.sex = ' W '; printf ("age =%d,sex =%c\n", p1.age,p1.sex); P1 = p2; printf ("age =%d,sex =%c\n", p1.age,p1.sex); P1 = (struct) {ten, ' w '}; 1, does not support the overall assignment//2, struct type can not be used repeatedly struct{int age; char sex; } P3,P4 = {, ' M '}; return 0;}4. struct-Body scope
Inside a function, a struct type scope is like a local variable
struct type defined outside the scope of a global variable
Scope: Start from the defined line until the end of the file
Note: struct types cannot be declared.
The function of a struct type, except that it cannot be declared. Others are the same as normal variables
#include <stdio.h>struct person{ int age; char sex;}; int main (int argc, const char * argv[]) { struct person p2; P2.sex = ' s '; P2.age = ten; struct person{ int age; }; struct person p = {Ten}; printf ("Age =%d\n", p.age); { struct person p = {$}; struct monkey{ char *name;} ; } return 0;}
5. Structure array
struct array: array element is a struct-body array
Defining an array of struct bodies
struct type array name [number of elements];
Array element type array name [number of elements];
#include <stdio.h>//the struct type is defined in the external struct dog{char *name of the function in ordinary cases ; int age;}; void Test () { // int nums[] = {1,2,3,4}; The struct hypothesis is not initialized. It's all garbage .// The first way: to define an array of structs and then initialize the struct Dog dogs[5]; Dogs[0] = (struct Dog) {"Wang Wealth", 1}; Dogs[1].age = 1; Dogs[1].name = "rhubarb"; traverse struct array for (int i = 0;i < 5;i++) { printf ("age =%d,name =%s\n", dogs[i].age,dogs[i].name); C17/>}}void test2 () { // define struct-body array at the same time to initialize // Assume that there is no explicitly initialized struct. Then all members of this structure will be initialized to 0 struct Dog dogs[10] = {"Wang Fu", 1},{"rhubarb", 2},{"to bless", 3},{"small black", 4},{"small white", 5}; computes the number of elements of an array int len = sizeof (dogs)/sizeof (struct Dog); for (int i = 0;i < Len; i++) { printf ("age =%d,name =%s\n", dogs[i].age,dogs[i].name); } } int main (int argc, const char * argv[]) { // test (); Tset2 (); return 0;}
6. Structure and pointer
struct pointer: pointer to struct body
The data type that the pointer points to * pointer variable name;
struct type * pointer variable name;
#include <stdio.h>struct student{ char *name;//name int no;//study number double score;//score};int main (int argc, const char * argv[]) { struct Student stu = {"Big wood", 60,59}; define struct-body pointer struct Student *SP1; SP1 = &stu;// defines the same time to initialize the struct Student *sp = &stu;//The first way to access the members of the struct through the pointer (*SP). score = 60; // Another way (emphasis) sp->name = "small wood"; Sp->no =; printf ("name =%s,no =%d,score =%.2lf\n", stu.name,stu.no,stu.score); return 0;}
7. Structure nesting
There can be other types of struct members inside a struct
Note points for structure nesting:
1. Struct cannot nest itself, cannot have a member of a type such as Byte
2, the structure can nest their own type of pointer
#include <stdio.h>//defines a date structure of struct time{int hour;//HH int minute;//mm int second;//ss};struct date{ int year; int month; int day; struct time time;}; struct employee{int no; Char name[20];//Date of entry//int year;//int month;//int day; struct Date indate;//birthday/int birthyear;//int birthmonth;//int birthday; struct Date birthday;//separation date//int goyear;//int gomonth;//int goday; struct Date outdate;}; void Test () {struct Employee emp = {1, ' big screen ', {2014,10,14,{12,12,12}},{1990,10,14},{2014,11,14}}; printf ("no =%d,name =%s, entry date =%d-%d-%d%d:%d:%d\n", Emp.no,emp.name,emp.indate.year,emp.indate.month,emp.inda Te.day, Emp.indate.time.hour,emp.indate.time.minute,emp.indate.time.second); }struct person{char *name;//struct person son; struct person *son;};/ * 1. There are 5 students in a class, three classes. Write 3 functions to achieve the following requirements: (1) The average score of each course, (2) to find out more than two failed students, and output their number and failed course results. (3) Find out the students with average score of 85-90 in each class and output their student number and name */int Main (int argc, const char * argv[]) {struct person father = {"Father"}; struct person son = {"Son"}; Father.son = &son; printf ("Son name =%s\n", father.son->name); return 0;}8. Enumeration
Enumeration: One by one lists out
Enumeration effect: Eliminate Magic numbers
Usage scenario: When something only has a few values, it's enumerated.
Defining enumeration formats
Enum Enum type name {
element,//comma
...
};
Note the point:
1. In the enumeration type definition. All elements are shaped constants
2, the essence of enumeration type is plastic
#include <stdio.h>//defines the gender enumeration type enum sex{man, Woman, other};//int man = 10;//enumeration After definition, cannot be defined after the same variable as member void Test ( {//In code that shows the special meaning of the number, our magic number/int sex = 0; enum sex sex; printf ("%d\n", man); int man = 10; printf ("%d\n", man); Sex = 1;//It is not possible to assign a number to a variable of an enumeration type, so that the meaning of the enumeration does not exist//man = 1; }//is able to specify each element value in the enumeration enum season{Spring = 2, Summer, Autumn, Winter, sother = 9};void Printseason (enum Season Season {switch (season) {case Spring: The value after//case must be the same as the member in the enumeration, do not appear the magic number printf ("Spring \ n"); Break Case summer:printf ("Summer \ n"); Break Case autumn:printf ("Autumn \ n"); Break Case winter:printf ("Winter \ n"); Break Case sother:printf ("not the season on earth \ \"); Break Default:break; }}int Main (int argc, const char * argv[]) {printf ("%d,%d,%d\n", Man,woman,other); printf ("%d,%d,%d,%d\n ", spring,summer,autumn,winter); Enum Season Season = Summer; Printseason (season); return 0;}
9. typedef:
Alias an existing data type
Basic data types
int char float double
Structural body
Enumeration
Pointer
1. Pointers to normal variables
2, the structure of the pointer
3. Function pointers
Basic data types
Definition Format: TypeDef has a data type alias;
#include <stdio.h>void testbasetype () {typedef int int; Int num = 10; typedef INT Integer; Integer a = 20; printf ("%d\n", a); }/* to struct type alias */void teststruct () {///First: Define struct type and then alias struct _person{int age for struct type; Char *name; }; typedef struct _PERSON person; Person P1 = {10, "Little Red"}; printf ("age =%d,name =%s\n", p1.age,p1.name); Another way to define struct type is to alias a typedef struct _dog{int to the struct type at the same time; Char *name; } Dog; Dog dog = {1, "rhubarb"}; The third Way: Define the anonymous struct type at the same time give the struct an alias typedef struct{INT age; char * name; } Cat; Cat cat = {1, "Tom"}; }//give the piece up alias Void Testenum () {///1, first define the enumeration type and then give the alias enum _sex{Man, Woman, and other}; typedef enum _SEX Sex; Sex sex = man;//2, defining an enumeration type at the same time gives an alias to the enumeration type typedef enum _season{Spring, Summer} Season; Season Season = Spring; 3. Define anonymous enumeration type raise one individual at the same timeThe name typedef enum {Cong, Suan, Jiang} Tiaowei; Tiaowei Zuoliao = Cong; Pointer of}//4, pointer type//4.1 basic data type void Testtypedefbasicpointer () {typedef int * INTPOINTERTYPE; int a = 10; Intpointertype p = &a; *p = 20; printf ("%d\n", a); }//4.2 the pointer to struct type alias void Testtypedefstructpointer () {typedef struct {char *name; int age; } person; typedef person * PERSONPOINTERTYPE; Person p = {"Zhang San", 10}; Personpointertype pp = &p; typedef struct _cat{char *name; int age; } Cat, * catpointertype; Cat cat = {"Kitten", 1}; Catpointertype Catpointer = &cat; printf ("name =%s,age =%d\n", catpointer->name,catpointer->age); }//4.3 alias for enumeration type Testtypedefenumpointer () {//First define enum type enum _sex{man, Woman, and other}; typedef enum _SEX * SEXPOINTER; Enum _sex Sex = man; Sexpointer Sexpointer = &sEx *sexpointer = Woman; printf ("%d\n", sex); }//4.4 the function pointer to an int sum (int num1,int num2) {return num1 + num2;} int minus (int num1,int num2) {return num1 + num2;} An alias to the function pointer//typedef the return value type of the function pointed to by the pointer (* function pointer type alias) (the list of functions pointed to); void Testtypedefmethodpointer () {//Int (*sumpointer) (in T num1,int num2) = sum;////int (*minuspointer) (int num1,int num2) = minus; typedef int (*methodpointertype) (int num1,int num2); Methodpointertype sumpointer = sum; Methodpointertype minuspointer = minus; int rs = Sumpointer (10,20); printf ("rs =%d\n", RS); }//understand void Testtypedefarraypointer () {char names[][10] = {"XTF", "ZBZ", "WF"}; char (*arraypoiner) [10]; Arraypoiner = names; typedef char (*ARRAYPOINTERTYPE) [10]; Arraypointertype pointer = names; printf ("%s\n", pointer[0]); }int Main (int argc, const char * argv[]) {////testbasetype ();//Teststruct (); Testtypedefbasicpointer (); TesTtypedefstructpointer (); Testtypedefenumpointer (); Testtypedefmethodpointer (); Testtypedefarraypointer (); return 0;}
iOS Learning tour------Play structure