snake_case,常數 UPPER_SNAKE,型別/結構體 PascalCase,巨集 UPPER_SNAKE。if/for/while 單行也加括號,防維護錯誤。// 宣告即初始化 (C99 允許混合宣告)
int i = 0, j = 0;
int count = 0;
double pi = 3.14159;
// 多變數同類型
int x = 10, y = 20, z = 30;
// 結構體初始化 (C99 指定初始化器)
struct Circle { double r; };
struct Circle c = { .r = 5.0 };
// 陣列初始化
int a[5] = {1,2,3}; // 剩餘自動為 0
char str[] = "hello"; // 自動計算長度含 \0
// 錯誤:未初始化使用
int x; printf("%d", x); // 未定義行為
// 錯誤:VLA 作為函數參數
void foo(int n, int arr[n]); // 不可移植,C11 可選特性
// 錯誤:區域靜態陣列大小過大
void bar() { int big[1000000]; } // 棧溢位風險
#include <stdio.h>
#define PI 3.1415926
int main() {
double r = 2.5;
double area = PI * r * r;
printf("半徑 %.2f 面積 %.2f\n", r, area);
// 簡易堆疊
int stack[10], top = -1;
#define PUSH(x) do { if (top < 9) stack[++top] = (x); } while(0)
#define POP() (top >= 0 ? stack[top--] : -1)
PUSH(1); PUSH(2); PUSH(3);
while (top >= 0) printf("%d ", POP());
return 0;
}
const 防止誤修改,static 限制連結可見性。這份筆記整理了 C 語言變數基礎與工程級編碼規範要點,適合入門者建立良好習慣,也可作為代碼審查核對清單。