首先,看看变量的存储:

int global ;
int main()
{
int stackStore ;
int heapStore* = (int *)malloc(sizeof(int));
}

变量global存储在全局数据存储区,stackStore存储在栈中,heapStore存储在堆中;

static作为静态修释符用法:

1.static可以用来修饰变量,也可以用来修饰函数,其用法相似;

2. static可以静态的呈现一个变量,在作用范围内不会改变变量的值;

3. 但是如果函数的局部变量复写了变量的值,那么这个值在当前局部函数内有效; 若出了当前局部范围,static的值生效;

例一, static在全局范围内,用include扩展static的作用范围, 用extern扩展函数的作用域:

107.h

#ifndef _107H_
#def _107H_ extern void func();
#endif

107.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "107.h" voic func()
{
x = 12;
printf("%d\n", x);
}

108.h

#ifndef _108H_
#def _108H_ extern void func1();
#endif

108.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "108.h" voic func1()
{
x = 56;
printf("%d\n", x);
}

109.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "107.h"
#include "108.h" int main()
{
func();
func1();
printf("%d", x);
}

输出结果为:

例二,static在当前文本作用域,用extern扩展函数的作用域:

file1.h

#ifndef _FILE1_
#define _FILE1_ extern void func1();
extern void func2(); #endif

file1.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" static char* hello = "hello world!";
void func1()
{
printf("%s\n", hello);
} void func2()
{
hello = "changed world!";
printf("%s\n", hello);
}

file2.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" int main()
{
func1();
func2(); return ;
}

输出结果:

!!! 若将static char* hello = "hello world!"放入func1, 如下;

file1.cpp

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "file1.h" void func1()
{
static char* hello = "hello world!";
printf("%s\n", hello);
} void func2()
{
hello = "changed world!";
printf("%s\n", hello);
}

那就会出错,错误为:

最新文章

  1. 关于GPL的一些知识
  2. Java中用内存映射处理大文件
  3. KVM 内存虚拟化
  4. git-配置公司账号
  5. 腾讯云centos 7部署 dotnetcore+Angular2 实践
  6. C#开发BHO程序(引)
  7. J2EE如何生成验证码图片和点击刷新验证码
  8. ls 显示目录和文件的技巧
  9. MongoDb注意事项
  10. 实践 config drive - 每天5分钟玩转 OpenStack(170)
  11. Python:名片管理系统(增加登录功能后出现问题,求教)
  12. [国嵌攻略][173][BOA嵌入式服务器移植]
  13. win10下配置php环境变量
  14. Java——总结
  15. 测试那些事儿—软测必备的Linux知识(四)
  16. nginx 日志 cron任务切割日志
  17. SprirngBoot微服务之间的交互—— restTemplate
  18. pop
  19. Java中clone的写法
  20. DAG最长路问题 hdu-1224

热门文章

  1. shared_ptr智能指针
  2. 15. window.onload和$(function() { } )的区别
  3. em 与 rem 区别.
  4. webApi的控制台服务
  5. hive安装 jdbc链接hive
  6. linux 运维基础之 禁止 ping
  7. elastic 集群安装
  8. select简单示例,有注释
  9. 解决ios、微信移动端的position: fixed; 支持性不好的问题 &amp;&amp; 禁用下拉暴露黑底的功能
  10. Apache Beam的目标