正在学习C语言,用的IDE是visual studio 2022
头文件里的定义还看不懂,所以来询问
用scanf_s()获取浮点数时输入i会被吞掉一个
但获取整数不会吞i
详情见下图
获取浮点数
#include <stdio.h>
float getnumber(void) {
float number = 0.0;
char ch;
while (scanf_s("%f", &number) != 1) {
while ((ch = getchar()) != '\n') {
putchar(ch);
}
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
return number;
}
int main(void) {
float num;
num = getnumber();
printf("%g\n", num);
return 0;
}
获取整数
#include <stdio.h>
int getnumber(void) {
int number = 0.0;
char ch;
while (scanf_s("%d", &number) != 1) {
while ((ch = getchar()) != '\n') {
putchar(ch);
}
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
return number;
}
int main(void) {
int num;
num = getnumber();
printf("%d\n", num);
return 0;
}
搜了一下没搜到,想问一下为什么和可以解决吗
这是特性还是bug呢?
会不会和i是虚数单位有关?