auto register static extern 变量
2012-09-16 21:31
295 查看
mylib.h
mylib.c
3. main.c
#ifndef MYLIB_H_ #define MYLIB_H_ extern double PI; extern int acc_add(int n); extern void print_city(); #endif /* MYLIB_H_ */
mylib.c
#include<stdio.h> #include<stdlib.h> //all external/global variables locate in data segment double PI = 3.1415; //external variable can be accessed form other file with "extern double PI;" //not allow other file to access the static external variable static char *city = "shanghai_beijing"; //static external variable can only be accessed in this file int acc_add(int n){ //n is a local variable which locates in stack, after exe, it disappears //local variable, located in stack, after function, it disappears, "auto" is usually omitted //static local variable, which is initialized in compiling phase //it locates in "data segment", after function, it is still there //if not initialized apparently, compiler initiate it with 0 by default static int total = 0; printf("before exec, total = %d, n = %d\n", total, n); total += n; return total; } void print_city(){ puts(city); } // static variable //1. static + local variable, its space exists all the execution time //2. static + external/global variable, only can be accessed in this file
3. main.c
#include <stdio.h> #include <stdlib.h> #include "mylib.h" int main(void) { printf("acc_add(1) = %d\n", acc_add(1)); printf("acc_add(1) = %d\n", acc_add(1)); printf("PI = %f\n", PI); print_city(); return EXIT_SUCCESS; }
相关文章推荐
- 变量属性(auto、register、static、extern)
- extern、static、auto、register 定义变量的不同用法
- c语言变量的存储方式(auto,register,static,extern)及作用域(局部变量/全局变量)详解
- C语言中局部变量和全局变量的存储类别(static,extern,auto,register)
- 函数 - 关于变量(auto、static、register、extern、volatile、restrict)
- extern、static、auto、register 定义变量的不同用法
- C语言-变量的存储类别(auto, static, register, extern)
- C语言中局部变量和全局变量变量的存储类别(static,extern,auto,register)
- C语言中局部变量和全局变量变量的存储类别(static,extern,auto,register)
- C语言变量存储类型auto,static,extern,static extern,register
- 学点 C 语言(34): 函数 - 关于变量(auto、static、register、extern、volatile、restrict)
- C语言变量存储类型auto static extern static extern register
- 变量的存储类别-auto/static/register/extern
- 变量的存储类别-auto/static/register/extern
- C++中auto,static,register,extern四种变量简单理解
- C变量(auto、static、register、extern)函数(内部、外部)与存储
- C语言变量存储类型auto,static,extern,static extern,register
- static auto extern register变量存储类型
- 关于常见的变量修饰符: const, static, extern, register, auto 的详细解释,使用场合以及注意事项
- C语言变量存储类型auto,static,extern,static extern,register