C语言---const和static

来源:互联网 发布:软件项目成本核算 编辑:程序博客网 时间:2024/04/30 12:29

//

//  main.c

//  conststatic

//

//  Created by GET-CC on 14-12-6.

//  Copyright (c) 2014 GET-CC. All rights reserved.

//


#include <stdio.h>

//1.

//static用来修饰全局变量:

static const int PI = 3.14;//PI只能在被模块内的函数使用

//2.

//static用来修饰函数:

staticvoid study(void);//此函数只能狗在本模块内或者本类使用,不能够被外部函数调用

void print(void);

void Unmove(void);

int main(int argc, const char * argv[])

{

    printf("Hello, World!\n");

    return 0;

}

//const用法

void print(void){

    //1.

   //定义一个只读的变量 =常量

   //const和基本数据类型的关键字是等价的,级别一致,所以下面的也可以写成:int const a = 10

    const int a = 10;//所以修饰的是:const aa是一个只读常量

    int ip = 9;

    

    //2.1

    const int *p = &ip;//修饰的是const *p,所以*p是不可变的,也就是*p所指向的内存内容是不可变的。

    ip = 11;//可以赋值

    //*p = 11;//不可以赋值

    p++;//可以自增

    //2.2

    int* const pi = &ip;

    //pi++;//不能够自增,因为cons修饰的是pi

    *pi = 100;//可以操作

    

    //2.3

    const int *const pt = &ip;

   //pt++;//结合上面的两个,所以这两个都不能够赋值成功。

    //*pt = 100;//


}

//static用法

void Unmove(void){

//3.修饰局部变量:

   staticint age =18;//这个变量的适用范围仍是这个函数内,但是生命周期和整个程序一样长

}

0 0