任何人都可以指出产生这些错误输出语句的内容

时间:2017-07-17 21:50:15

标签: c visual-studio

编译此程序时,输出包含错误的语句。任何人都可以指出可能导致这种情况的原因吗?

#include <stdio.h>
int main(void)
{
int num1;
int num2;

printf("enter two numbers and I will tell you the relation\n");
printf("the relation they satisfy\n");
scanf_s("%d%d" , &num1, &num2);

if (num1 == num2) {
printf("%d is equal to %d\n", num1, num2);
}

if (num1 != num2) {
printf("%d is not equal to %d\n", num1, num2);
}

if (num1 > num2); {
printf("%d is greater than %d\n", num1, num2);
}

if (num1 < num2); {
printf("%d is less than %d\n", num1, num2);
}

if (num1 >= num2); {
printf("%d is greater than or equal to %d\n", num1, num2);
}

if (num1 <= num2); {
printf("%d is less than or equal to %d\n", num1, num2);
}
}

编译之后,输入例如25和50作为我的数字。它返回语句:

  • “25不等于50”
  • “25大于50”
  • “25小于50”
  • 25大于或等于50
  • 25小于或等于50

2 个答案:

答案 0 :(得分:3)

if (num1 > num2); {
//              ^ extra semi colon
printf("%d is greater than %d\n", num1, num2);
}

这意味着:如果条件为真,则不执行任何操作 然后无条件地执行阻止。

答案 1 :(得分:0)

对于每个if语句,都有一个额外的分号(;)。这意味着if条件语句已停止执行,并且下一个printf语句正在执行而没有应用任何条件