# 變數範圍

``````const double PI = 3.14159;

doule area(double r) {
return r * r * PI;
}

int main(void) {
// .....
return 0;
}
``````

``````for(int i = 0; i < 100; i++) {
// ....
}
``````

``````int i = 10;
for(int i = 0; i < 100; i++)  {
// ...
}
printf("%d\n", i);
``````

``````#include <stdio.h>

void count();

int main(void) {
for(int i = 0; i < 10; i++) {
count();
}

return 0;
}

void count() {
static int c = 1;
printf("%d\n", c);
c++;
}
``````

``````1
2
3
4
5
6
7
8
9
10
``````

foo.c

``````double v = 1000;
// ... 其他定義
``````

main.c

``````#include <stdio.h>

int main() {
extern double v;
printf("%f\n", v);

return 0;
}
``````

``````#include <stdio.h>

int main() {
extern double v = 2000; // error, `v' has both `extern' and initializer
// ...
return 0;
}
``````

``````#include <stdio.h>

int main() {
extern double v;
v = 2000;
// ...

return 0;
}
``````

``````extern void foo();
``````

foo.c

``````static double v = 1000;
// ... 其他定義
``````

``````// 定義在 .h 中
static void foo() {
...
}
``````