用static声明外部变量

有时希望某些外部变量只限于被本文件引用,
而不能被其他文件引用
这时,可以在定义外部变量时加一个static声明。


file1.c

include <stdio.h>

int A; /这里我们增加了static不让别的文件引用/

void main()
{
int power(int); /函数声明/
int b = 3, c, d, m;

  printf("enter the number a and its power m:
");
  scanf("%d %d", &A, &m);
  
  c = A * b;
  printf("%d * %d = %d
", A, b, c);

  d = power(m);
  printf("%d ^ %d = %d
", A, m, d);

}


file2.c

extern A; /声明A为一个已定义的外部变量/

int power(int n)
{
int i, y = 1;

  for(i=1; i <= n; i++)
  {
        y *= A;
  }

  return y;

}

原文地址:https://www.cnblogs.com/poli/p/4529603.html