static和extern修饰连接属性
来源:互联网 发布:java参数传递构造函数 编辑:程序博客网 时间:2024/04/24 13:59
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和extern修饰连接属性
- static和extern修饰全局变量
- static和extern修饰函数
- extern和static修饰全局变量的区别
- static和extern修饰局部变量
- static & extern 修饰函数
- c语言中extern和static修饰的全局变量
- static修饰属性
- static、extern和extern "c"
- extern,static,const修饰变量的用法
- objectiveC【语法】修饰符 static extern const
- objectiveC【语法】修饰符 static extern const
- C语言 修饰符(static、extern)
- objectiveC【语法】修饰符 static extern const
- 修饰符 static extern const (转载)
- static、extern、无修饰符全局变量…
- objectiveC修饰符 static extern const
- objectiveC【语法】修饰符 static extern const
- 323. Nothing in the world is difficult for one who sets his mind to it. 世上无难事,只怕有心人
- MSSQL求連續ID內數量合計-常見解決方案
- 静态SQL与动态SQL的比较
- 对字符串常量的修改
- java中代码检查checkStyle结果分析 .
- static和extern修饰连接属性
- static的作用
- C库文件说明
- Lustre I/O性能特点
- hdu 4339 不错的线段树
- 雅可比算法求方阵的全部特征值和特征向量
- 黑马程序员---入学考试分析(金额转换)
- 开发的第一个iphone应用--HandPhoto
- android sax dom pull 解析 xml