這個問題在這里已經有了答案: C 中 bool 資料型別的 scanf 中的格式說明符 1 個答案 昨天關門。
有時我在練習編碼時使用 stdbool.h。這時候如果scanf的格式修飾符給定為%d,就會出現如下錯誤資訊。
c:\project\hello\hello\hello.c(11): 警告 C4477: 'scanf' : 格式字串 '%d' 需要 'int *' 型別的引數,但可變引數 3 的型別為 'bool'
它似乎可以編譯,但在運行時似乎無法正確識別真/假或 0/1 輸入。我想知道是否有什么我錯過了。
uj5u.com熱心網友回復:
您正在將bool( 或_Bool) 傳遞給scanf. 使用時%d,你應該傳遞一個int.
如果你bool是 named x,那么使用:
int temporary;
if (1 != scanf("%d", &temporary))
{
fprintf(stderr, "Error, scanf did not work as expected.\n");
exit(EXIT_FAILURE);
}
x = temporary;
(對于exit,插入#include <stdlib.h>您的源代碼。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460623.html
