gpt4 book ai didi

c - 使用 scanf 返回值验证输入类型失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:03 24 4
gpt4 key购买 nike

我想通过使用 scanf 的返回值检查此代码中的所有输入是否都是 double 类型:


#include <stdio.h>

int main()
{
double a,b,c;
int x = scanf("%lf %lf %lf", &a, &b, &c);
printf("%d", x);
}

但这就是我得到的:

  • 在这里我期望 1 但我得到 0:
    enter image description here
  • 在这里我期望 2 但我得到 1: enter image description here

最佳答案

根据 scanf man

The format string consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a directive fails, no further input is read, and scanf() returns


因此

对于案例 1。

scanf 在检测到第一个说明符不匹配时将停止读取。

对于案例 2。

scanf 将在检测到第二个说明符不匹配时停止读取。

关于c - 使用 scanf 返回值验证输入类型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55732975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com