本文共 2666 字,大约阅读时间需要 8 分钟。
static 是一个用来控制变量储存方式及可见性的关键字,常用于修饰变量和函数。它的使用位置决定了变量或函数的性质,包括静态函数、静态全局变量和静态局部变量。
静态函数
static void fun(void) { ...}void fun(void) { ...} 静态全局变量
static Robot_t Robot;Robot_t Robot1;
静态局部变量
void fun(void) { static uint8_t robot; uint8_t robot1;} | 关键字 | 使用周期 | 作用域 |
|---|---|---|
| extern | 编程执行 | 外部(整个程序) |
| static | 编程执行 | 内部(仅目标文件) |
| auto、register | 功能执行 | 无 |
volatile uint8_t robot;register uint8_t robot1;
const int a = 0;int const b = 0;
int const *a;const int *b;int *const c;const int *const d;
void test(const Class& Var); // 引用参数不可修改void test(const TYPE& Var); // 引用参数为常量
__asm volatile("BKPT #01");__asm void SystemReset(void) { MOV R0, #1 MSR FAULTMASK, R0 LDR R0, =0xE000ED0C LDR R1, =0x05FA0004 STR R1, [R0] deadloop: B deadloop} __align(32) volatile CPU_INT08U External_RamMemp[EXTERNAL_MEM_NUM][EXTERNAL_MEMBLOCK_SIZE] __attribute__((at(0x08004000)));
typedef __packed__ { uint8_t a : 1; uint8_t b : 1; uint8_t c : 1; uint8_t d : 1; uint8_t e : 1; uint8_t res : 3;} StructTypef;typedef struct __attribute__((__packed__)) { uint8_t a; char b;} StructTypef; inline Robot_t* Robot_Get_Base(void) { return &Robot;} __irq void USART2_IRQHandler(void) { if (USART2->ISR_USART_ISR_RXNE) { USART2->ISR &= ~USART_ISR_RXNE; cyclic_buffer_push(&s_comm.s_cyclic_rx_, USART2->RDR); }} void __attribute__((weak)) f();int main(void) { if (f) { f(); return 0; }} 通过以上标识符的学习与应用,我们能够更好地理解 C/C++ 代码的特性,提升代码的安全性和性能。更多技术文章请关注 良知犹存的技术博客。