声明与初始化

声明告诉编译器类型与名字;初始化给出起始值。工程上二者常写在一行。

1. 例子对比

C
1int count; // 声明,值不确定
2int total = 0; // 声明并初始化(推荐)
3int a = 1, b = 2, c = 3;
4int x, y;
5x = 10; y = 20; // 也可先声明后赋值

2. 拆解

3. 实际例子:单位换算变量

C
1uint32_t adc_raw = 2048; // 12bit 中间值
2const float vref = 3.3f;
3const float full = 4095.0f;
4float vout = (float)adc_raw / full * vref;
5printf("vout=%.3fV\n", vout);