static函数局部变量的使用

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


void staticFun(void)
{
   static  uint8_t  data = 0;
   data++;
   data++;
   printf("static function data = %d\r\n",data);
}

void NostaticFun(void)
{
   uint8_t  data = 0;
   data++;
   data++;
   printf("no static function data = %d\r\n",data);
}

int main()
{
//static 功能
  printf("局部static的功能\r\n");
  staticFun();
  staticFun();
  staticFun();
  staticFun();

//没有statuc的功能
  printf("没有static的功能\r\n");
  NostaticFun();
  NostaticFun();
  NostaticFun();
  NostaticFun();

  return 0;
}

static函数局部变量保存这上一次的内容依次递增,而没有static的变量每次调用函数,都只是从0开始递增。

打印结果如下:

static函数局部变量的使用