当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C语言中,static关键字作用

C语言中,static关键字作用

2019年07月28日  | 移动技术网IT编程  | 我要评论

免费电子图书下载,哪种石斛最好,美人泪山河恋

static修饰变量

1 在块中使用static修饰变量

  • 它具有静态存储持续时间、块范围和无链接。
    即作用域只能在块中,无法被块外的程序调用;变量在程序加载时创建,在程序终止时结束。
  • 它只在编译时初始化一次。如果没有显式初始化,默认初始化为0.
#include <stdio.h>

void trystat(void);

int main(void)
{
   int count;
   for (count = 1; count <= 3; count++)
   {
           printf("here comes iteration %d:\n", count);
           trystat();
   }
   
   return 0;
}


void trystat(void)
{
   int fade = 1;
   static int stay = 1;
   
   printf("fade = %d and stay = %d\n", fade++, stay++);
}

程序执行结果
here comes iteration 1:
fade = 1 and stay = 1
here comes iteration 2:
fade = 1 and stay = 2
here comes iteration 3:
fade = 1 and stay = 3

(1) 这里变量stay ,它从程序加载时开始存在,直到程序终止。但是它的范围仅限于trystat()函数块。只有当这个函数执行时,程序才能使用stay访问它指定的对象.

(2) 变量stay记得它的值增加了1,但是变量fade每次都会重新开始。这指出了初始化的不同之处:fadetrystat()每次调用都从新初始化,而stay变量只初始化一次。

(3) 静态变量在程序加载到内存之后就已经就位了。将语句static int stay = 1;放在trystat()函数中告诉编译器,只允许trystat()函数查看变量;它不是在运行时执行的语句。

2 在任何函数外部使用static修饰变量

  • 它具有静态存储时间、文件范围和内部链接。
    即作用域在当前文件之中(只能被同一文件中的函数使用),无法被其他文件调用
  • 它只在编译时初始化一次。如果没有显式初始化,默认初始化为0。

使用static修饰函数

  • 作用域限制在当前定义的文件中使用,从而避免了多文件函数名称冲突的可能性。通常文件中不作为接口的函数,建议使用static修饰,这样避免不同文件使用相同的函数名发生冲突。
static bool wavtaskcreated = false;
static queuehandle_t wav_msg_queue = null;
static wav_play_queue_t wavplayqueue;

static bool wav_get_version_flag = false;
static char wav_version[32];

static uint8_t *wav_getfilename(wav_type_t wav_type)
{

}


static bool wav_getfileinfo(wav_file_info_t *pfileinfo, uint8_t *pfilename)
{
  
}


static bool wav_getversion_internal(wav_file_info_t *pfileinfo)
{

}


static bool wav_readfile(uint8_t *pdata, uint32_t offset, uint32_t size)
{

}


static bool wav_sendtoda(uint8_t *pfile, uint32_t size,  uint32_t volume)
{
 
}


static bool wav_put_queue(wav_type_t wav_type, bool fromisr)
{
 
}


static bool wav_get_queue(wav_type_t *pwavtype)
{

}


static bool wav_play_inernal(wav_type_t wav_type)
{

}


void wav_init(void)
{
    da_init(16000);

    if (!wavtaskcreated)
    {
        wavtaskcreated = true;
        xtaskcreate(wav_task, "wav", stack_size_task_wav, null, priority_task_wav, null);
        wav_msg_queue = xqueuecreate(wav_msg_queue_length, sizeof(wav_message_t));

        wavplayqueue.pwavtypetable = malloc(wav_paly_queue_max * sizeof(wav_type_t));
    }

    wavplayqueue.front = 0;
    wavplayqueue.end = 0;
    wavplayqueue.sizenow = 0;
    wavplayqueue.totalsize = wav_paly_queue_max;
}


bool wav_play(wav_type_t wav_type, bool force)
{
    if (!wavtaskcreated)
    {
        return false;
    }

    if (force)
    {
        vportentercritical();

        watchdog_feed();
        
        wav_play_inernal(wav_type);
        
        vportexitcritical();

        return true;
    }
    
    bool rv = wav_put_queue(wav_type, false);

    vtaskdelay(30);

    return rv;
        
}

上述为某平台使用da播放wav的程序片段,

static uint8_t *wav_getfilename(wav_type_t wav_type);
static bool wav_getfileinfo(wav_file_info_t *pfileinfo, uint8_t *pfilename);
static bool wav_getversion_internal(wav_file_info_t *pfileinfo);
static bool wav_readfile(uint8_t *pdata, uint32_t offset, uint32_t size);
static bool wav_sendtoda(uint8_t *pfile, uint32_t size,  uint32_t volume);
static bool wav_put_queue(wav_type_t wav_type, bool fromisr);
static bool wav_get_queue(wav_type_t *pwavtype);
static bool wav_play_inernal(wav_type_t wav_type);

上述函数为内部函数使用static修饰。

void wav_init(void);
bool wav_play(wav_type_t wav_type, bool force);

上述两个函数为模块接口(初始化da和播放wav功能)供外部调用,没有使用static修饰。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网