static的用法
首先,看看变量的存储:
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);
}
那就会出错,错误为:
static的用法的更多相关文章
- Java中static的用法
static静态,作为修饰符,最初是由c引入,一开始static表示退出一个块后依然存在的局部变量.随后,static表示不能被其他文件访问的全局变量和函数.到了C++和java,static表示属于 ...
- ava下static关键字用法详解
Java下static关键字用法详解 本文章介绍了java下static关键字的用法,大部分内容摘自原作者,在此学习并分享给大家. Static关键字可以修饰什么? 从以下测试可以看出, static ...
- C# static的用法详解
C# static的用法详解 有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 n ...
- static之用法
本文转载于http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html 在C语言中,static的字面意思很容易把我们导入歧途,其实它 ...
- static关键字用法
java中static关键字可用于修饰: 1.属性:表示该属性变量在类被加载时即被创建并初始化,类加载过程只进行一次,因此静态变量也只被创建一次 2.方法:静态方法为类的公有方法,可直接用‘类名.方法 ...
- static、final、static final 用法
1.使用范围:类.方法.变量.2.区别和联系:2.1.static 含义:静态的,被 static 修饰的方法和属性只属于类不属于类的任何对象.2.2.static 用法:2.2.1.static 可 ...
- typedef,static,const用法
一.typedef主要功能是定义一个已存在类型的别名,但是和宏并存 宏与typedef区别 1.宏定义只是简单的字符串替换 2.typedef定义的类型是类型的别名,typedef后面是一个整体声明, ...
- C/C++中static的用法全局变量与局部变量
1.什么是static? static 是C/C++中很常用的修饰符,它被用来控制变量的存储方式和可见性. 1.1static的引入 我们知道在函数内部定义的变量,当程序执行到它的定义处时,编译器为它 ...
- C/C++中关键字static的用法及作用
本文将主要从static在C和C++共有的作用及C++特有的作用两个方面进行解析. 在C和C++中共有的作用 隐藏(对变量.函数均可) 当同时编译多个文件时,所有未加static前缀的全局变量或全局函 ...
随机推荐
- DOM_02之查找及元素操作
1.查找之按节点间关系查找周围元素: 2.查找之HTML属性:①按id查找:var elem=document.getElementById("id"):找到一个元素,必须docu ...
- SQLServer查看死锁
SQLServer查看死锁 if exists ( select * from sys.procedures where name like '%USP_ShowLocks%' ) drop proc ...
- cordova填坑
cordova填坑
- CCNA网络工程师学习进程(5)路由器和交换机的登录安全配置和vlan划分
本节详细介绍路由器和交换机的登录安全配置以及VLAN划分的原理. (1)登录安全配置: 路由器登录有两种验证方式:有本地验证方式和远程验证方式.本地登录验证方式可以配置用户名和密码也可 ...
- 【工具】CodeSmith Generator 7.0.2激活步骤
学过三层的人应该认识CodeSmith Generator吧,今天我就跟大家一起探讨下CodeSmith Generator 7.0.2的激活,这最新版本破解的难度也是超越以往......具体看这篇日 ...
- TTAS Lock C++11 实现
template<class Lock> class Lock_guard{ private: Lock lock; public: explicit Lock_guard(Lock&am ...
- javascript类型系统——undefined和null
× 目录 [1]原因 [2]undefined [3]null 前面的话 一般的程序语言,表示空的只有null,但javascript的设计者Brendan Eich却设计了一个undefined,这 ...
- CSS选择器的一些记录
选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id="firstna ...
- 学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...
- 帮助你实现漂亮界面的14套免费的 HTML/CSS 源码
在网络上能找很多免费的 PSD 格式素材,但是很少有 HTML/CSS 界面组件下载.在这篇文章中,收集了14套免费的 HTML/CSS 界面源码分享给前端设计师和开发者们.这些组件包括按钮.滑块.表 ...