1 #include <stdio.h>
2 //const :是一个关键字,作用定一个常型变量
3 //使用: const 类型 变量名 = 初始值;
4 //注意事项:1.必须赋初始值 2.程序执行期间只读不可写
5
6
7
8 /**********************************
9 * define 定义的常量 与const 修饰的变量有什么区别
10 * #define NUM 200
11 * const int NUM = 200;
12 * 区别:define 不做类型检查,而const作类型检查
13 */
14 #define NUM "XXXX"
15 const int NUM_2 = 200;
16 int main()
17 {
18 int a = NUM_2;
19 printf("%d\n",NUM_2);
20 printf("%s\n",NUM);
21
22 int num = 10;
23 int xxx = 50;
24 int *p = #
25
26 *p = 20;
27
28 //常量指针
29 const int *q = # //说明 const 修饰的是 *q,那么*q定义以后是不可以被赋值的。但是q没有被修饰,那么q是可以赋值的
30
31 //指针常量
32 int *const w = &xxx; //说明 const 修饰的是 w,那么w不能被赋值,但是*w可以被赋值
33
34 //常量指针常量
35 const int *const t = q; //const *t,const t都去修饰所以*t不可写,t也不可写
36
37 printf("*t = %d\n",*t); //20
38
39 //w = &xxx;
40 *w = 100;
41 printf("xxx = %d\n",xxx);
42
43
44
45 return 0;
46 }