C语言中scanf --- 对于c99的_Bool应该用什么格式?
C语言的c99标准不是增加了_Bool么,那么在用scanf输入时应该用哪个格式符?我试了,不是 %d和%c
c99应该引入了新的格式符吧(话说%d、%c这是叫格式符吧)
百度也找不到结果
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
bool flag;
scanf("%c", &flag);
return 0;
}
main.c:7:10: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
bool flag;
scanf("%d", &flag);
return 0;
}
main.c:7:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
没办法的,不过可以用0或1 来代替
#include <stdbool.h>
#include <stdio.h>
bool b;
int temp;
scanf("%d", &temp);
b = temp; 小甲鱼老师?
@小甲鱼
^_^ 本帖最后由 Croper 于 2019-6-9 15:28 编辑
翻了下fscanf的说明,应该是没法直接scanf吧,毕竟你要用户输入"true"或者"false"感觉还是太怪异了
(图片的顺序反了。。不知道怎么弄正) Croper 发表于 2019-6-9 15:27
翻了下fscanf的说明,应该是没法直接scanf吧,毕竟你要用户输入"true"或者"false"感觉还是太怪异了
(图片 ...
那对于gcc来说,既然能识别 _Bool *,应该会有对应的格式?
main.c:7:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=] Croper 发表于 2019-6-9 15:27
翻了下fscanf的说明,应该是没法直接scanf吧,毕竟你要用户输入"true"或者"false"感觉还是太怪异了
(图片 ...
对于输入 “true”和“false”
这个可以效仿C++,输入0和非0表示true和false
#include <iostream>
int main(void)
{
bool flag;
std::cin >> flag;
std::cout << flag << std::endl;
return 0;
}
Seawolf 发表于 2019-6-12 11:27
没办法的,不过可以用0或1 来代替
好吧,那就不用bool作为输入了 人造人 发表于 2019-6-12 13:10
好吧,那就不用bool作为输入了
查了一下文档,似乎 scanf 没有针对 _Bool 添加对应的格式化符号。 小甲鱼 发表于 2019-6-12 14:12
查了一下文档,似乎 scanf 没有针对 _Bool 添加对应的格式化符号。
嗯,谢谢老师
^_^
页:
[1]