gpt4 book ai didi

c - 如何在 scanf 中一次插入 2 个其他值

转载 作者:行者123 更新时间:2023-11-30 18:40:08 27 4
gpt4 key购买 nike

我的 numB 中的数字不正确。

如何在 scanf 中一次插入 2 个其他值?还是做不到?

#include <stdio.h>
main()
{
char a,b;
int numA,numB;

printf("A : ");
scanf("%c%d",&a,&numA);

printf("B : ");
scanf("%c%d",&b,&numB);

printf("\n\n%d %d",numA,numB);
}

结果

A : S 67
B : D 56


67 1684370524

最佳答案

问题是读入 b 的值是您输入 67 后按下的换行符(Enter 键) 。然后阅读numB失败,因为它尝试解释 DnumB .

如果您输入 S 67 D 56 <Enter> (即中间没有 Enter)那么你会得到正确的输出。

要解决此问题,一种方法是将格式字符串更改为 " %c%d" 。空格意味着在尝试读取字符之前它将消耗所有空格。

关于c - 如何在 scanf 中一次插入 2 个其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095750/

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