//
// main.c
// 05-static和局部变量
//
// Created by apple on 13-7-1.
// Copyright (c) 2013年 apple. All rights reserved.
//
/*
static:
1.对函数
* 定义一个内部函数
static void test()
{
}
* 声明一个内部函数
static void test();
2.对全局变量
* 定义一个内部变量
static int a;
3.对局部变量
* 定义一个生命周期较长的局部变量
void test()
{
static int a = 0;
}
*/
#include <stdio.h>
//int count;
void test()
{
/*
默认情况下,局部变量的生命周期
1.调用所在的函数时,才会创建
2.函数调用完毕,就会销毁
*/
int a = 0;
a++;
printf("a = %d\n", a);
/*
static修饰的局部变量的生命周期:
1.第一次使用这个变量的时候才会创建,只会做一次初始化
2.程序退出的时候,才会销毁
*/
static int count = 0;
count++;
printf("count=%d\n", count);
// count++;
}
double zhouchang(double radius)
{
// 2 * pi * radius
static double pi = 3.14;
static int num = 2;
return num * pi * radius;
}
int main(int argc, const char * argv[])
{
for (int i = 0; i<5; i++)
{
test();
}
//printf("count=%d\n", count);
return 0;
}
黑马程序员10——C语言之static和extern
最新推荐文章于 2014-09-03 22:55:46 发布