static, #define define constants, macros, and pointers

Table of Contents

static

#define defines constants and macros

pointer


static

static is used to modify variables and functions

1. Modify local variables – called static local variables

2. Modify global variables – called static global variables

3. The modified function is a static function.

//Code 1
#include <stdio.h>
void test()
{
    int i = 0;
    i + + ;
    printf("%d ", i);
}
int main()
{
 int i = 0;
    for(i=0; i<10; i + + )
   {
        test();
   }
    return 0;
}
//Code 2
#include <stdio.h>
void test()
{
    //static modifies local variables
    static int i = 0;
    i + + ;
    printf("%d ", i);
}
int main()
{

 int i = 0;
    for(i=0; i<10; i + + )
   {
        test();
   }
    return 0;
}

/*Compare the effects of code 1 and code 2 to understand the meaning of static modified local variables.
in conclusion:
Static modification of local variables changes the life cycle of the variable
Let static local variables still exist out of scope, and the life cycle will not end until the program ends.
11.2.2 Modify global variables
Code 1 is normal, but code 2 will cause a connectivity error during compilation.
in conclusion:
A global variable is modified static, so that the global variable can only be used in this source file and cannot be used in other source files.
use.
*/

Compare the effects of code 1 and code 2 to understand the meaning of static modified local variables.
in conclusion:
Static modification of local variables changes the life cycle of the variable
Let static local variables still exist out of scope, and the life cycle will not end until the program ends.
static modifies global variables

//Code 1
//add.c
int g_val = 2018;
//test.c
int main()
{
    printf("%d\
", g_val);
    return 0;
}
//Code 2
//add.c
static int g_val = 2018;
//test.c
int main()
{
    printf("%d\
", g_val);
    return 0

Code 1 is normal, but code 2 will cause a connectivity error during compilation.
in conclusion:
A global variable is modified static, so that the global variable can only be used in this source file and cannot be used in other source files.
use.

static modified function

//Code 1
//add.c
int Add(int x, int y)
{
    return c + y;
}
//test.c
int main()
{
    printf("%d\
", Add(2, 3));
    return 0;

}
//Code 2
//add.c
static int Add(int x, int y)
{
    return c + y;
}
//test.c
int main()
{
    printf("%d\
", Add(2, 3));
    return 0;
}

Code 1 is normal, but code 2 will cause a connectivity error during compilation.
in conclusion:
A function is modified static, so that the function can only be used in this source file and cannot be used in other source files.
The remaining keywords will be explained in subsequent courses.

#define Define constants and macros

//define define identifier constants
#define MAX 1000
//define definition macro
#define ADD(x, y) ((x) + (y))
#include <stdio.h>
int main()
{
    int sum = ADD(2, 3);
    printf("sum = %d\
", sum);
    
    sum = 10*ADD(2, 3);
    printf("sum = %d\
", sum);
    
    return 0;
}

pointer

1
Memory

Memory is a particularly important memory on a computer. Programs in the computer are all run in memory.

So in order to use memory effectively, the memory is divided into small memory units. The size of each memory unit is
1
bytes
.

In order to effectively access each unit of memory, the memory units are numbered. These numbers are called the
Memory unit location
Address
.

Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses.

Get the variable address as follows:

#include <stdio.h>
intmain(){
intnum=10;
 &num;//Get the address of num
    //Note: There are 4 bytes of num here, each byte has an address, and the address of the first byte is taken out (the smaller address)
printf("%p\
", & amp;num);//Print address, %p is printed in the form of address
return0;
}

How to store the address requires defining a pointer variable.

int
num
=
10
;

int*
p
;
//p
is an integer pointer variable

p
= &
num
;

Examples of using pointers:

#include

int
main
()

{

int
num
=
10
;

int*
p
=&
num
;

*
p
=
20
;

return
0
;

}

Taking integer pointers as an example, it can be extended to other types, such as:

#include

int
main
()

{

char
ch
=
‘w’
;

char*
pc
=&
ch
;

*
pc
=
‘q’
;

printf
(
“%c\

,
ch
);

return
0
;

}

Size of pointer variable

#include

//
The size of the pointer variable depends on the size of the address

//32
The address under the platform is
32
indivual
bit
bit (i.e.
4
bytes)

//64
The address under the platform is
64
indivual
bit
bit (i.e.
8
bytes)

int
main
()

{

printf
(
“%d\

,
sizeof
(
char*
));

printf
(
“%d\

,
sizeof
(
short*
));

printf
(
“%d\

,
sizeof
(
int*
));

printf
(
“%d\

,
sizeof
(
double*
));

return
0
;

}

Conclusion
: pointer size is 32
bit platform is
4
bytes,
64
bit platform is
8
bytes.

Structure:

The structure is
C
A particularly important knowledge point in the language, the structure makes
C
The language has the ability to describe complex types.

For example, describing students, students include:
name
+
age
+
gender
+
student ID
these pieces of information.

This can only be described using structures.

For example:

struct
Stu

{

char
name
[
20
];
//
name

int
age
;
//
age

char
sex
[
5
];
//
gender

char
ID
[
15
]
;
//
student ID

};

Initialization of the structure:

//
Print structure information

struct
Stu s
=
{

Zhang San

,
20
,

male

,
“20180101”
};

//.
Access operator for structure members

printf
(
“name = %s age = %d sex = %s id = %s\

,
s
.
name
,
s
.
age
,
s
.
sex
,
s
.
ID
);

//->
Operator

struct
Stu
*
PS
=&
s
;

printf
(
“name = %s age = %d sex = %s id = %s\

,
PS
->
name
,
PS
->
age
,
PS
->
sex
,
PS

>
ID
);