static局部变量
下面这个示例主要是演示static关键字修饰一个局部变量时的作用。
#include<stdio.h>
#include <unistd.h>
int f();
int *p;
/*
* gcc staticStorageSpecifer.c -o test -std=c99
*/
int main() {
for (int i = 0; i < 5; i++) {
printf("method f() return value is:%d.\n", f());
}
// 即使在方法里将局部变量声明为静态了,还是不能在方法外部直接使用它。
//printf("get variable value: %d.\n", series_num);
printf("sleep 5 second.");
sleep(5);
printf("method f() return value is:%d.\n", f());
/*
* 下面这三行是想测试拿到一个局部static变量的指针,然后模拟清空。
*/
printf("static variable pointer address: %p\n", p);
*p = 0;
printf("method f() return value is:%d.\n", f());
return 0;
}
/**
* 在调用之间保持局部变量值,如下面的方法中将series_num声明为static
*/
int f() {
static int series_num = 0;
series_num += 23;
p = &series_num;
return series_num;
}
输出结果如下:
method f() return value is:23.
method f() return value is:46.
method f() return value is:69.
method f() return value is:92.
method f() return value is:115.
sleep 5 second.method f() return value is:138.
static variable pointer address: 0x60104c
method f() return value is:23.
static全局变量
下面这个示例主要是演示static关键字修饰一个全局变量时的作用。
#include<stdio.h>
static int series_num;
void series_start(int);
int serier();
/**
* 全局变量前加static令全局变量成为全局静态变量,使之仅在定义该变量的
* 文件中是可见的.因此虽然变量是全局的,但其他文件中的子程序无法感知其
* 存在,无法修改之,这样就有效消除了全局变量的副作用。
*
* gcc staticStorageSpecifer2.c -o test
*/
int main() {
printf("test one block!\n");
series_start(10);
printf("method serier return value is:%d.\n", serier());
printf("test two block!\n");
series_start(20);
printf("method serier return value is:%d.\n", serier());
}
void series_start(int num) {
series_num = num;
}
int serier() {
series_num += 20;
return series_num;
}
输出结果如下:
test one block!
method serier return value is:30.
test two block!
method serier return value is:40.
static全局变量(2)
定义一个staticStorageSpecifer3.h头文件。
#include<stdio.h>
int serier();
再定义一个staticStorageSpecifer3.c源文件。
#include "staticStorageSpecifer3.h"
static int series_num;
int serier() {
series_num += 20;
return series_num;
}
最后定义一个testGlobalStatic.c文件。
//
// Created by tuzhao on 18-3-2.
//
// gcc testGlobalStatic.c staticStorageSpecifer3.c -o test
//
// 测试如何使用在其它.c文件中定义的全局static变量
#include "staticStorageSpecifer3.h"
int main() {
printf("get value from static storage specif 3: %d\n.", serier());
return 0;
}
输出结果如下:
get value from static storage specif 3: 20
static方法
下面主要是测试static修饰方法。
首先定义一个static_method.h头文件。
#include <stdio.h>
static int call();
void use();
接着定义一个static_method.c源文件。
#include "static_method.h"
static int call() {
printf("method call() from static_method.c\n");
return 88;
}
void use() {
call();
}
最后定义一个test_static_method.c文件。
//
// Created by tuzhao on 18-3-2.
//
// gcc static_method.c test_static_method.c -o test
//
#include "static_method.h"
int call();
/*
* 主要是测试static关键字修饰方法
*
* c中的static关键字有点类似于java中的private的关键字,被static修饰的
* 关键字只能在自己的.c文件中被调用。其它的.c文件是看不见它的,但是其它的
* .c文件仍然可以通过下面的use()方法来间接调用它。
*
*
* 我们可以在当前的.c文件中声明自己的call()方法,而不会与static_method.h中
* 定义的static call()方法冲突。
*/
int main() {
printf("test static method.\n");
use();
call();
}
int call() {
printf("method call() from test_static_method.c\n");
return 99;
}
输出结果如下:
test static method.
method call() from static_method.c
method call() from test_static_method.c
gcc version
tuzhao@tuzhao-linux:~/code/c/baseThree/static$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
本文由 tuzhao 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2018/03/04 08:57