简明C:为什么这个扫描码不起作用

  • 本文关键字:扫描 不起作用 简明 c
  • 更新时间 :
  • 英文 :


在C中可以合并打印和扫描吗?我试着运行这段代码,它并没有要求任何输入,只是输入了一些垃圾值。

#include <stdio.h>
int main()
{
int a, b, c;
scanf("Enter first number: %d", &a );
scanf ("Enter the second number: %d", &b);
scanf("Enter the third number: %d", &c);
printf("%d == %d is %d n", a, b, a == b);
printf("%d == %d is %d n", a, c, a == c);
printf("%d > %d is %d n", a, b, a > b);
printf("%d > %d is %d n", a, c, a > c);
printf("%d < %d is %d n", a, b, a < b);
printf("%d < %d is %d n", a, c, a < c);
printf("%d != %d is %d n", a, b, a != b);
printf("%d != %d is %d n", a, c, a != c);
printf("%d >= %d is %d n", a, b, a >= b);
printf("%d >= %d is %d n", a, c, a >= c);
printf("%d <= %d is %d n", a, b, a <= b);
printf("%d <= %d is %d n", a, c, a <= c);
return 0;
}

传递给scanf的字符串(格式字符串(是而不是将要打印的内容。它查找的是输入

所以当你这样做的时候:

scanf("Enter first number: %d", &a );

它寻找类似于"0"的输入;输入第一个数字:42〃;如果你只是键入一个像";42〃;CCD_ 2将不匹配任何内容,因此不会向CCD_ 3分配任何值。

你可能想要:

printf("Enter first number: ");
if (scanf("%d", &a ) != 1) 
{
// error - the input did not match an int
exit(1);
}
// now a contains the scanned number
printf("The scanned value is %dn", a);

最新更新