char a;
scanf_s("%c", &a); // 警告 C4477:format string '%c' requires buffer size
printf("%c", a);
scanf_s 是 Microsoft 安全版本,要求 %c、%s、%[ 等格式必須提供緩衝區長度(以字元數為單位)。
char a;
scanf_s("%c", &a, 1); // 第 3 參數:緩衝區大小(字元數)
// 或
scanf_s(" %c", &a, 1); // 前置空格跳過換行/空白
scanf 寫法char a;
scanf(" %c", &a); // 空格跳過空白字元,無需長度參數
若使用 scanf_s 只為通過編譯器檢查,建議統一改用 scanf + 空格前綴,跨平台相容性更好。
scanf("%c", &a) 會讀到上一行殘留的 \n。scanf_s("%s", buf) 同樣需長度:scanf_s("%s", buf, sizeof(buf))。scanf_s("%c", &ch, 1) 或 scanf(" %c", &ch),記得處理前導空白字元。