代码改变世界

static和extern修饰局部变量/修饰全局变量/修饰函数

2015-07-14 01:41  另十  阅读(1398)  评论(0)    收藏  举报

-static和extern修饰局部变量

 staticextern 修饰局部变量
  
 
      static修饰局部变量:
         //作用:1)static定义的变量的作用域会得到延长,从程序启动到程序退出,但它并没有改变变量的作用域
                   局部的static实际也是全局的
         
         //     2) static定义变量的语句只会被执行一次
 
      extern是不能够修饰局部变量的
 
 
#include <stdio.h>
/**
 *
 */
void sum(){

    //没有使用static修饰
    int num = 0;
    printf("num = %d\n",num);  //0
    num++;

}

void sum1(){
    //static 可以修饰局部变量
    //作用:1)static定义的变量的作用域会得到延长
    //     2) static定义变量的语句只会被执行一次
    static int num = 0;
    printf("num = %d\n",num);  //0
    num++;
    
}


void sum2(){
   
    //extern int num = 0;
    int num = 0;
    printf("num = %d\n",num);  //0
    num++;
    
}

int main(int argc, const char * argv[]) {
    
    
    sum();
    sum();             //再次调用sum(),但上次调用时所占内存已经被释放了
    sum();
    
    printf("------------\n");
  
    sum1();
    sum1();
    sum1();
    
    return 0;
}
num = 0
num = 0
num = 0
------------
num = 0
num = 1
num = 2

 

static和extern修饰全局变量

  static 对全局变量的作用(内部变量)
 
          static修饰的全局变量,表示该全局变量只能在当前文件中使用,而不能在其他文件中使用
    
          在不同的文件中,使用static可以声明的同名的变量
 
    extern 对全局变量的作用
 
          extern修饰的全局变量,表示当前变量可以在本文件中使用,也可以在
          其他文件中使用
 
          注意:
          1)extern声明的全局变量在不同的文件中不能同名(两个文件须由包含关系)
          
 

 (一)

test.c

#include "test.h"
//定义一个静态的全局变量
static int num = 10;
extern int a=10;

void test1(){

    printf("test1 num = %d\n",num);
}

void test2(){

    printf("test2 a = %d\n",a);

}

test.h          //头文件一般都是用来声明变量和函数

#ifndef __C14_______test__
#define __C14_______test__

#include <stdio.h>
static int num; //声明一个静态变量
void test1();
void test2();
extern int a;
#endif /* defined(__C14_______test__) */

 

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

int main() {
   
    printf("num = %d\n",num);    //结果是num=0,也就是被static修饰的全局变量,表示该全局变量只能在当前文件中使用,而不能在其他文件中使用
//调用test.c中的函数

test1();

     printf("  a = %d\n ",a);


test2();

return 0;
}

 

    num = 0                     //
 test1 num = 10
    a = 10                      // extern修饰的全局变量,表示当前变量可以在本文件中使用,也可以在其他文件中使用
test2 a = 10

(二)

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

  static int num = 15;  //可以声明同名变量
  //extern int a=15;     //编译不通过,因为 test.c文件中已经定义a了    printf("num = %d\n",num);

    printf("num = %d\n",num);

   return 0;
}
num = 15

(三)

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

 extern int a;                    ------------------->假如头文件中没有声明 extern int a; 那可以这里声明,全局声明,在在全局的时候extern可以省略
int main() {

extern int a; ------------------->假如头文件中没有声明 extern int a; 那也可以这里声明,这里不能省略


printf(
"num = %d\n",num);

return 0; }

(四)

#include <stdio.h>
#include <stdlib.h>
#include "test.h"
static int num = 15;  //声明同名变量

//不能声明不存在的变量,比如 extern int x;会编译不通过
 int x; //全局变量默认的初始化为0 int main() { printf("x = %d\n",x);  //结果是0 return 0; }

 

-static和extern修饰函数

        static 和 extern对函数的作用
 
    static 修饰的函数,是一个内部函数(只能在定义的那个文件中去使用)
 
    extern 声明的函数,可以在定义文件中使用,也可以在其他文件中使用

 

#ifndef __C14_______test__
#define __C14_______test__

#include <stdio.h>
static int sum(int a,int b);
float avg(int a,int b);
//extern int max(int a,int b);

#endif /* defined(__C14_______test__) */
#include "test.h"

static int sum(int a,int b){

    return a+b;
}

float avg(int a,int b){

    return sum(a, b)/2.0f;

}

extern int max(int a,int b){

    return a>b?a:b;

}
#include <stdio.h>
#include "test.h"

int main(int argc, const char * argv[]) {
    
    //int s = sum(223, 12);          //sum被static修饰就不能在这里调用,
    float av = avg(223, 12);         //av函数里面使用sum,因为av sum同在一个文件,所以可以调用
    //printf("s = %d\n",s);
    printf("av = %.2f\n",av);
    
    //调用之前作个声明
    extern int max(int a,int b); // 这是在头文件没声明的时候
    int m = max(23, 88);
    printf("m = %d\n",m);
    
    return 0;
}
av = 117.50
m = 88