------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Knowledge Point 24: Static variable, global variable declaration but not initialized with initial value (Int0 char)
But the local variable declaration is not initialized and there is no initial value.
Knowledge Point 25: The overall initialization of a struct must be made at the time of declaration
[CPP]View Plaincopy
- struct person P1 = {"Zhangsan", ' F '};//this is correct
The following is the wrong
[CPP]View Plaincopy
- struct person p2;
- P2 = {"Zhangsan", +, ' F '};
Initialization can also be written like this
[CPP]View Plaincopy
- struct person p1 = {. Name = "Zhangsan",. Age =,. Gender = ' F '};
Knowledge Point 26: The memory occupied by the struct is a multiple of the maximum member's memory (completion algorithm), as
[CPP]View Plaincopy
- struct student{
- int AGE;//4 bytes
- char*name;//pointer type of eight bytes
- };
- struct Student stu;
- Stu.age = 20;
- Stu.name = "Jack";
- int s = sizeof (STU);
- printf ("%d\n", s);//output is 16 principle: 4+8=12--->16
Knowledge Point 27: Several ways to define structure variables:
Way One:
[CPP]View Plaincopy
- struct student{
- int age;
- Char *name;
- };
- struct Student stu1;
Way two:
[CPP]View Plaincopy
- struct student{
- int age;
- Char *name;
- }stu2;//This is defined as a variable at the same time as a type, can only be written once, otherwise the type is defined repeatedly
Way three:
[CPP]View Plaincopy
- struct {
- int age;
- Char *name;
- }stu3;//This method cannot again define this type of variable
One of the best ways for a person to think:
[CPP]View Plaincopy
- typedef struct student{
- int age;
- Char *name;
- }student;
- Student Stu4;
Knowledge Point 28: Preprocessing directives (three types):
1. Macro definition
2. Conditional compilation
3. The file contains
1. Macros defined by pairing and using macros with parameters
[CPP]View Plaincopy
- #define
- ...
- #undef
Macros with parameters
[CPP]View Plaincopy
- #define SUM (V1,V2) ((v1) + (v2))//parentheses are required
For example:
[CPP]View Plaincopy
- #define Pingfang (a) a*a
- #define Pingfang (a) (a*a)
When called
[CPP]View Plaincopy
- Pingfang (Ten)/pingfang (2)//Incorrect
- Pingfang (5+5)//Incorrect
Macro efficiency with parameters is higher than function
2. Conditional compilation (usually judging the value of a macro)
[CPP]View Plaincopy
- #if conditions
- ...
- #elif conditions
- ...
- #else
- ...
- #endif (very important) otherwise all subsequent code is invalid
[CPP]View Plaincopy
- #ifdef is equivalent to
- #if defined ()
[CPP]View Plaincopy
- #ifndef is equivalent to
- #if!defined ()
3. The file contains
1.<> represents the file that comes with the system, "" represents a custom file
2. Do not allow loop inclusions, such as A.H contains B.h,b.h and contains A.H
Knowledge Point 29:typedef is just an alias for a type, not a new type.
[CPP]View Plaincopy
- struct student{
- int age;
- Char *name;
- };
- typedef struct STUDENT Student;
Equivalent to
[CPP]View Plaincopy
- typedef struct student{
- int age;
- Char *name;
- }student;
is also equivalent to
[CPP]View Plaincopy
- typedef struct {
- int age;
- Char *name;
- }student;
Similar to naming enumeration types
[CPP]View Plaincopy
- typedef enum SEX
- {
- Man,women
- }sex;
The following scenario is more specific
The following is the custom data type for the function pointer type, and the return value type and parameter type to match
[CPP]View Plaincopy
- #include <stdio.h>
- typedef int (*typefuncpointer) (int, int);
- int add (int a, INTB)
- {
- return a + B;
- }
- int minus (int a, INTB)
- {
- return a-B;
- }
- int main ()
- {
- Typefuncpointer p = add;//using a custom type
- Typefuncpointer P2 = minus;//using a custom type
- printf ("Add =%d\n", p (1, 2));
- printf ("Minus =%d\n", p2 (1, 2));
- return 0;
- }
Here is the pointer type that defines the struct body
[CPP]View Plaincopy
- typedef struct student{
- int age;
- Char *name;
- }*ptrstu;
- How to use
- Student Stu ={18, "Zhangsan"};
- Ptrstu p = &stu;
A macro definition is also a type that can be named
[CPP]View Plaincopy
- #define Integer INT
Equivalent
[CPP]View Plaincopy
- typedef int Integer
Attention and the difference between typedef
For example:
[CPP]View Plaincopy
- typedef char * String
- #define STRING2CHAR *
- Use
- String s1,s2;//s1 and S2 are char * types
- String2 s3,s4;//s3 is of type char*, but S4 is of type Char
Knowledge point 30:static and extern difference is the possibility of cross-file access
1. Functions
2. Variables
Effects on functions
External functions: Defined functions can be accessed by this file and other files
internal functions; Defined functions can only be accessed by this file
By default, all functions are external (equivalent to the keyword extern), so you can omit the extern effect:
Complete definitions and references an external function is added extern
Reference is also the default external function, so also omit extern
Static function: Define an intrinsic function
Use: Static return type function name (parameter list)
cannot be called by another file
In a project, the external function name of this file cannot be the same as the external function of other files (error)
The intrinsic function (static) in this file can be the same as the function name of other files.
Effect on a variable:
There are two categories of global variables:
External variables: Defined variables can be accessed by other files
1. By default, all global variables are external variables
2. External variables of the same name in different files represent the same
3. Define an external variable without extern and add extern to the declaration
The same statement is not wrong.
Internal variable: The defined variable cannot be accessed by another file
Different files with the same name internal variables do not affect each other
Dark Horse programmer--c Language Foundation---the easy omission in C language learning (Part I)