我正在尝试解决这个 HackerRank 挑战:
已经声明了 3 个变量:i(整数)、d(双精度)和 s(字符串)。
我必须声明另外 3 个与前一个类型相同的变量,使用 scanf() 请求输入。
然后打印 i 与第二个 int 变量的和。然后打印 d 与第二个 double 变量的总和。
最后,我无法完成的唯一任务是将 s 字符串与第二个 char 变量(字符串)连接起来。
前两个任务顺利完成,但是串联没有运行。
我只得到第一个字符串和 0 作为输出。(HackerRank 0。)
这是代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;`your text`
double d = 4.0;
char s[] = "HackerRank ";
int secondI;
float secondD;
char s2[100];
scanf("%d", &secondI);
scanf("%1f", &secondD);
scanf("%s", s2);
printf("%d\n",i + secondI);
`your text`
printf("%.1f\n", d + secondD);
printf("%s ", s);
printf("%s", s2);
return 0;
}
```
`
Expected Output
16
8.0
HackerRank is the best place to learn and practice coding!
My Output (stdout)
16
8.0
HackerRank .0
scanf("%1f", &fvar)
读取 one 字符并将该单个字符解释为写入变量的浮点值。其余的输入将用于下一个 scanf()
。
将
scanf("%f", &fvar)
与浮点数一起使用...或 scanf("%lf", &dvar)
与双精度数一起使用。
并检查scanf的返回值
if (scanf("%lf", &dvar) != 1) /* error in input */;