【发布时间】:2010-12-17 18:07:21
【问题描述】:
我正在学习 C。
我正在使用 ubuntu 并有 Code::Blocks 作为 IDE 我有这个代码:
#include <stdio.h>
int rev (int num);
int main (){
int numb = 0;
printf("%d\n\n", numb);
printf("Please enter a number. Enter 9999 to stop\n");
scanf("%d", &numb);
printf("there?");
printf("%d\n", numb);
while (numb != 9999){
printf("The reversed number is %d\n", rev(numb));
printf("Please enter a number. Enter 9999 to stop\n");
scanf("%d", &numb);
} /* end of while */
}
int rev (int num){
printf("here?");
int total = 0;
long max = 10;
long max_const = 10;
printf("here");
for (max; max < num; max *= 10);
printf("%ld", max);
max_const = max;
for (int i = 0; i <= max_const; i *= 10, max /= 10){
total += num / max * i;
} /* end for */
return total;
}
我这样做是因为我的书不清楚...但是,问题是它在 scanf 中引发了浮点异常...我正在输入正常数字...奇怪问题是,如果我输入除 9999 之外的所有内容,程序就会崩溃。如果我输入 9999,它会打印“那里?” (所以 scanf 没关系)并稍后停止,显然。为什么?
谢谢。
【问题讨论】:
-
因此,如果您键入 9999 以外的任何内容,它会崩溃而不打印“那里”? “崩溃”是什么意思?它有什么作用?段错误?你用调试器单步执行了吗?
-
@Falmarri:大概会产生浮点异常...
-
没错,它会产生一个浮点异常。如果我输入 9999,它会在那里打印吗?并停下来。如果我输入其他内容,则会引发浮点异常
标签: c exception floating-point scanf