The Role of static Keyword in C Language

Keywords: Programming

static modifier variable

1 Modify variables in blocks with static

  • It has static storage duration, block range, and no links.
    That is, scopes can only be in blocks and can not be called by programs outside blocks; variables are created when programs are loaded and terminated when programs terminate.
  • It is initialized only once at compile time. If there is no explicit initialization, the default initialization is 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++);
}

Procedure execution results:
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) Here the variable stay exists from the time the program is loaded until the program terminates. But its scope is limited to the trystat() function block. Only when this function is executed can the program use stay to access the object it specifies.
(2) The variable stay remembers that its value increased by 1, but the variable fade starts again every time. This points out the difference in initialization: fade is reinitialized every time it is called in trystat(), while the stay variable is initialized only once.
(3) Static variables are in place after the program is loaded into memory. Statement static int stay = 1; placed in the trystat() function tells the compiler that only the trystat() function is allowed to view variables; it is not a statement executed at run time.

2 Modify variables with static outside any function

  • It has static storage time, file range and internal links.
    That is, scopes are in the current file (which can only be used by functions in the same file) and cannot be called by other files.
  • It is initialized only once at compile time. If there is no explicit initialization, the default initialization is 0.

Use static to modify functions

  • Scope is limited to the files currently defined, thus avoiding the possibility of conflicting names of multi-file functions. Normally, functions that are not used as interfaces in files are recommended to be modified with static to avoid conflicts between different files using the same function name.
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;
        
}

Above is a program clip that uses DA to play wav for a platform.

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 functions are decorated with static for internal functions.

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

These two functions are module interfaces (initialization DA and playback wav functions) for external calls without using static modification.

Posted by teckn1caLity on Sat, 27 Jul 2019 23:48:58 -0700