这个c程序怎么了?对其进行调试表明该程序在while(sqroot!= 0)时被击中; [英] What's wrong with this c program? Debugging it suggests that the program is struck at while (sqroot != 0);
问题描述
> 以下是调试过程的屏幕截图
我正在学习用C编程.我正在尝试查找数字是否为镜像,但是该程序编译时没有错误,但没有得到期望的结果.对该程序进行调试表明它在while(sqroot!= 0)时击中了
Here's a screenshot of debugging process
I am learning to program in C. I am trying to find if a number is a mirror image or not, But the program compiles error-free yet didn't give the desired result. Debugging the program shows that it struck at while (sqroot != 0);
// Mirror number
#include <stdio.h>
#include <math.h>
int main() {
int num, rev1, rev2, rem1, rem2, sqr, sqroot;
printf("Enter a number\n");
scanf("%d", & num);
sqr = pow(num, 2);
while (sqr != 0) {
rem1 = sqr % 10;
rev1 = rev1 * 10 + rem1;
sqr = sqr / 10;
}
sqroot = sqrt(rev1);
while (sqroot != 0); {
rem2 = sqroot % 10;
rev2 = rev2 * 10 + rem2;
sqroot = sqroot / 10;
}
if (rev2 == num)
printf("number is mirror");
else
printf("Not a mirror number");
return 0;
}
推荐答案
while (sqroot != 0); { ... }
是无限循环.因为;
被视为空指令.紧随if (condition)
或while (condition)
之后的指令(空或非空)被视为if
,while
,for
范围的 only 指令.
while (sqroot != 0); { ... }
is an infinite loop. because the ;
is considered as an empty instruction. An instruction (empty or not) just after an if (condition)
or a while (condition)
is considered to be the only instruction of the if
, while
, for
scope.
与写作相同
while (sqroot != 0)
{
; /* Do nothing */
}
/* The scope below doesn't belong to the while */
{
rem2 = sqroot % 10;
rev2 = rev2 * 10 + rem2;
sqroot = sqroot / 10;
}
删除;
,此问题应得到解决.
Remove the ;
and this problem should be solved.
while (sqroot != 0) {
rem2 = sqroot % 10;
rev2 = rev2 * 10 + rem2;
sqroot = sqroot / 10;
}
这篇关于这个c程序怎么了?对其进行调试表明该程序在while(sqroot!= 0)时被击中;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!