C language, static key role

static variables modified

1 using a modified static block variables

  • It has static storage duration, block scope and without links.
    That scope is only in the block, the block can not be invoked outside; variables are created when the program is loaded, ending when the program terminates.
  • It only initialized once at compile time. If you do not explicitly initialized, initialized by default to 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) where the variable stay, it is loaded from the start there is, until the program is terminated. However, the scope is limited to the trystat()function block. Only when this function is executed, the program can use stayto access it specified object.

(2) variable stayremembers its value increased by 1, but variable fade every time to start again. It pointed out the differences between the initialization: fadeIn trystat()each call re-initialization, and stayvariables are initialized only once.

(3) static variables in the program is loaded into memory after it is already in place. The statement static int stay = 1;on trystat () function tells the compiler that allows only trystat()function to see the variable; it is not a statement is executed at run time.

2 modified using static variables outside of any function

  • It has a static storage time, file scope and internal links.
    That scope (function can only be used in the same file) in the current file, can not be called by other files
  • It only initialized once at compile time. If you do not explicitly initialized, initialized by default to 0.

Use static modification function

  • Scope restrict the use of the file currently defined, so as to avoid the possibility of multi-file function name conflict. Usually file as a function of the interface is not recommended to use static modification, thus avoiding different files use the same function name conflict.
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;
        
}

Used above is a broadcast wav DA platform program fragment,

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);

The above-described function for internal use function staticmodifications.

void Wav_Init(void);
bool Wav_Play(WAV_TYPE_t wav_type, bool force);

It said two function module interface (DA initialization functions and play wav) for external calls, without the use of static modification.

Guess you like

Origin www.cnblogs.com/sky1blue/p/11260164.html