c语言的static用作函数链接限制
来源:互联网 发布:列车时刻表软件哪个好 编辑:程序博客网 时间:2024/06/06 01:51
c++ 中可以使用 namespace 来避免同名引发的错误,而c语言的大型项目中要做到这一点,就有点难度了,通常会使用static来修饰函数。
static 修饰函数声明时,表示该函数只用于本文件的调用范围。效果见下面的小例子:
情况1:
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
a.h :
#ifndef A_H_
#define A_H_
#include<stdlib.h>
#include<stdio.h>
static void func_print( int a );
#endif
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
b.c:
#include<stdlib.h>
#include<stdio.h>
void func_print(int a )
{
printf("2222 a = %d ", a);
}
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
a.c :
#include"a.h"
void func_print(int a )
{
printf("1111 a = %d ", a);
}
void main(void)
{
int a = 10;
func_print(a);
}
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
输出结果为: 1111 a = 10
情况2:
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
a.h :
#ifndef A_H_
#define A_H_
#include<stdlib.h>
#include<stdio.h>
static void func_print( int a )
{
printf("1111 a = %d ", a);
}
#endif
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
b.c:
#include<stdlib.h>
#include<stdio.h>
void func_print(int a )
{
printf("2222 a = %d ", a);
}
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
a.c :
#include<stdlib.h>
#include<stdio.h>
extern void func_print(int a);
void main(void)
{
int a = 10;
func_print(a);
}
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
输出结果为: 2222 a = 10
本人在解码库中会遇到各种同名的函数,弄个长长的名字来避免链接错误感觉忒别扭。使用static 是个不错的方法~
- c语言的static用作函数链接限制
- c语言函数加static的意义
- C语言的Static Inline函数
- c语言的链接属性和static、extern关键字
- C语言 - 链接字符串函数的使用
- grep -c的用作
- C语言中的 static变量、static函数
- C语言中的 static变量、static函数
- C语言中的 static变量、static函数
- C语言中的 static变量、static函数
- c语言static变量和static函数
- c语言sprintf的使用,可以用作itoa
- C语言--static修饰函数
- C语言中static 函数
- C语言中的static函数
- C语言中的static函数
- c语言中static 函数和普通函数的区别?
- c语言中static 函数和普通函数的区别?
- Oracle 索引 详解
- ORACLE中index的rebuild
- Oracle中数据库与实例的区别
- 再谈POSIX系统调用和ANSIC库函数区别
- Oracle同义词
- c语言的static用作函数链接限制
- Oracle视图
- ORACLE物化视图
- sql语法:inner join on…
- POI操作Excel常用方法总结
- 如何对Oracle数据库文件进行恢复与…
- Oracle数据库备份与恢复之一:exp/…
- 搭建Maven私服
- MySQL数据库备份和还原的常用命令