有时,我在练习编码时使用stdbool.h。此时,如果将扫描f的格式修饰符指定为%d,则会发生以下错误消息。
c:\project\hello\hello\hello.c(11):警告C4477:'scanf‘:格式字符串'%d’需要一个类型为'int *‘的参数,但变量参数3具有'bool’类型。
它似乎是编译的,但在运行时它似乎没有正确地识别true/false或0/1输入。我想知道我是不是错过了什么。
发布于 2022-04-19 23:39:18
您正在将一个bool (或_Bool)传递给scanf。使用%d时,应该传递int的地址。
如果您的bool名为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://stackoverflow.com/questions/71932326
复制相似问题