static和extern修饰连接属性
2012-10-31 08:03
387 查看
1. static修饰全局变量或函数,表示其只能在所在文件中使用
extern修饰变量,表示其来自于其他文件
---------------------------------------
[root@localhost cpp]# cat source.c
#include<stdio.h>
static int height = 8848; // can only be used in this file
double pi = 3.14; //can be accessed from other file under the same project
int getHeight(){ // can be accessed from other file under the same project
return height;
}
--------------------------------------------
[root@localhost cpp]# cat main.c
#include<stdio.h>
int getHeight(); //or: external int getheight;
//extern double pi;
double getArea(double diameter){
extern double pi; // putting it here is also ok
return pi * diameter * diameter / 4;
}
int main(){
int height = getHeight();
printf("Height = %d\n", height);
double diameter = 2;
double area = getArea(diameter);
printf("diameter = %f, area = %f\n", diameter, area);
return 0;
}
-------------------------------------------
编译执行:
[root@localhost cpp]# gcc source.c main.c -o main
[root@localhost cpp]# ./main
Height = 8848
diameter = 2.000000, area = 3.140000
==========================================
2. extern的另外用法是当C和C++混合编程时,如果c++调用的是c源文件定义的函数或者变量,那么要加extern来告诉编译器用c方式命名函数:
文件A.cpp调用source.c里面的变量i和函数callme()
extern "C" //在c++文件里调用c文件中的变量
{
int j;
void callme();
}
int main()
{
callme();
}
二,static法则:
A、若全局变量仅在单个C文件中访问,则可以将这个变量修改为静态全局变量,以降低模块间的耦合度;
B、若全局变量仅由单个函数访问,则可以将这个变量改为该函数的静态局部变量,以降低模块间的耦合度;
C、设计和使用访问动态全局变量、静态全局变量、静态局部变量的函数时,需要考虑重入问题;
extern修饰变量,表示其来自于其他文件
---------------------------------------
[root@localhost cpp]# cat source.c
#include<stdio.h>
static int height = 8848; // can only be used in this file
double pi = 3.14; //can be accessed from other file under the same project
int getHeight(){ // can be accessed from other file under the same project
return height;
}
--------------------------------------------
[root@localhost cpp]# cat main.c
#include<stdio.h>
int getHeight(); //or: external int getheight;
//extern double pi;
double getArea(double diameter){
extern double pi; // putting it here is also ok
return pi * diameter * diameter / 4;
}
int main(){
int height = getHeight();
printf("Height = %d\n", height);
double diameter = 2;
double area = getArea(diameter);
printf("diameter = %f, area = %f\n", diameter, area);
return 0;
}
-------------------------------------------
编译执行:
[root@localhost cpp]# gcc source.c main.c -o main
[root@localhost cpp]# ./main
Height = 8848
diameter = 2.000000, area = 3.140000
==========================================
2. extern的另外用法是当C和C++混合编程时,如果c++调用的是c源文件定义的函数或者变量,那么要加extern来告诉编译器用c方式命名函数:
文件A.cpp调用source.c里面的变量i和函数callme()
extern "C" //在c++文件里调用c文件中的变量
{
int j;
void callme();
}
int main()
{
callme();
}
二,static法则:
A、若全局变量仅在单个C文件中访问,则可以将这个变量修改为静态全局变量,以降低模块间的耦合度;
B、若全局变量仅由单个函数访问,则可以将这个变量改为该函数的静态局部变量,以降低模块间的耦合度;
C、设计和使用访问动态全局变量、静态全局变量、静态局部变量的函数时,需要考虑重入问题;
相关文章推荐
- static 修饰的属性、方法、代码块的使用实例
- JAVA梳理(1)——static修饰属性的实际应用
- 注意this用法,以及当用static 修饰属性时,无论生成多少个对象,其共同拥有一个属性,不再各自拥有自己的属性
- 关于static和final修饰类的属性变量以及变量的初始化
- Java static修饰属性(类变量)、static修饰方法(类方法)
- Static和Final修饰类属性变量及初始化
- C++ VS C#(12):函数的重载,类成员的static修饰,属性
- Static和Final修饰类属性变量及初始化(转载)
- Java中static、final、static final修饰的属性
- Java 中被static 修饰的属性,方法和代码块在什么时候加载
- Java - Static和Final修饰类属性变量及初始化
- 变量属性(auto、register、static、extern)
- 黑马程序员—C学习笔记—static和extern修饰的函数和变量
- C++ VS C#(12):函数的重载,类成员的static修饰,属性
- static与final修饰类属性及初始化问题
- extern和static修饰全局变量的区别
- c语言的链接属性和static、extern关键字
- Java中final和static对修饰类、方法、属性的总结
- static和extern修饰局部变量
- static & extern 修饰函数