/*对用户输入的整数求和*/
#include <stdio.h>
int main(void)
{
long num;
long sum = 0L;
int status;
printf("Please enter an integer to be summed ");
printf("(q to quit): ");
status = scanf_s("%1d", &num);
while (status == 1)
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
status = scanf_s("%1d", &num);
}
printf("Those integers sum to %1d.\n", sum);
return 0;
}
为什么我的运行结果是不对的啊😂 变成了 1+2+2+3=8
while 语句里面还循环了两次
1
QBugHunter 2021-01-10 09:54:59 +08:00
while (status == 1)
????? |
2
Chowe 2021-01-10 10:18:09 +08:00 via iPhone 1
%1d 表示长度为 1 的整形数
所以 12 会变成 1 2 更多有关细节,请查阅格式化输入输出 |
3
commoccoom OP @Chowe 明白了,多谢!
|
4
msg7086 2021-01-10 15:45:34 +08:00
你是不是把 ld 打成了 1d ?
|
5
commoccoom OP @msg7086 好像是的啊😅
|