C 退出函数没有按照我的预期执行
当我使用调试器时,我可以知道退出不是退出函数。我使用退出功能错误吗? (我必须是)我该如何解决这个问题?
int is_prime(int x,char array[]){
int divider = (x-1);
float test;
while(x>-1){
test = isdigit((x % divider)); //isdigit returns !0 if digit
if(divider == '1'){
return(1); //if divider reaches 1 then the number is prime
exit;
}
if(test == '0'){
return (0);//not prime
exit;
}
divider--;
}
}
When I use a debugger I can tell the exit is not exiting the function. Am I using the exit function wrong? (i must be) how do I fix this?
int is_prime(int x,char array[]){
int divider = (x-1);
float test;
while(x>-1){
test = isdigit((x % divider)); //isdigit returns !0 if digit
if(divider == '1'){
return(1); //if divider reaches 1 then the number is prime
exit;
}
if(test == '0'){
return (0);//not prime
exit;
}
divider--;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
函数名称本身(后面没有括号)只是给出函数的地址,而不调用它。在 C 中,最基本的语句是一个表达式,它会评估其副作用,并忽略表达式的结果值。因此,像
exit;
或3;
这样没有副作用的语句是合法的,但实际上并没有做任何事情,最好删除。一些编译器会向您发出有关此类无意义语句的警告,尽管您可能必须打开额外的诊断警告才能获得它们。使用这样的选项(例如用于gcc
的-Wall
)是一个非常好的主意,并且将帮助您避免像这样的一些陷阱。The name of a function by itself (with no parenthesis after it) just gives you the address of a function without calling it. In C the most basic statement is an expression which is evaluated for its side effects, with the resulting value of the expression ignored. So a statement like
exit;
or3;
which has no side effects is legal but doesn't actually do anything and might as well be deleted. Some compilers will give you warnings about such meaningless statements, though you may have to turn on extra diagnostic warnings to get them. Using such options (such as-Wall
forgcc
) is a very good idea and will help you avoid some pitfalls like this.你必须调用它:
另外,如果你把它放在 return 之后,它永远不会被调用,因为 return 从函数返回。
编辑:并且,正如其他人所说, exit 退出程序,所以您可能不想在这里使用 exit 。
You must call it:
Also, if you put it after return, it will never be called, since return returns from the function.
EDIT: And, as others have said, exit exits the program, so you probably don't want to use exit here.
除了
return
和exit
的错误之外,您使用整数和字符的方式也存在错误。isdigit
是一个仅应用于字符的函数,如果字符位于'0'
和'9'
之间,则返回 true,但应该知道 C 中的字符表示法只是编写代码点(大多数时候是 ASCII)的一种奇特方式。因此,当您在 C 程序中编写'1'
时,编译器将看到49
,如果您编写'a'
编译器实际上会看到97
。这意味着isdigit
对于值48
到57
返回true
,可能不是您想要的。在将divider
与'1'
进行比较的行中,实际上您是将其与49
进行比较(IBM 大型机除外,其中 < code>'1' is241
)你的循环是无限的,它取决于 x 的值,但 x 在循环中没有改变,所以 while 中的条件永远不会改变。
编辑:这里是更正后的代码
Besides the bugs of
return
andexit
you have a bug also in the way you use ints and characters.isdigit
is a function that is only applied to characters, giving true if a character is between'0'
and'9'
, but one should know that the character notation in C is only a fancy way of writing a codepoint (ASCII most of the time). So when you write'1'
in a C program, the compiler will see49
, if you write'a'
the compiler sees in reality97
. This means thatisdigit
returntrue
for the values48
to57
, probably not what you intended. In the line where you comparedivider
with'1'
, in reality you're comparing it with49
(except on IBM mainframe, where'1'
is241
)Your loop is infinite, it depends on the value of x, but x isn't changed in the loop, so the condition in the while can never change.
EDIT: Here the corrected code
阅读
exit(3)
的手册。Read
exit(3)
's manual.该语句:
给出以下 GCC 警告:
发生的情况是,您有一个计算结果为 exit() 函数地址的表达式 - 但该表达式实际上并未对该地址执行任何操作。这类似于您有这样的陈述:
它是有效的 C,但它对任何东西都没有影响。
要调用该函数,作为 Thomas Padron -McCarth 说,你必须有参数列表(即使对于某些函数来说它们是空的):
The statement:
gives the following warning with GCC:
What happening is that you have an expression that evaluates to the address of the
exit()
function - but that expression doesn't actually do anything with that address. it's similar to if you had a statement like:It's valid C, but it has no effect on anything.
To call the function, as Thomas Padron-McCarth said, you have to have the argument list (even if they're empty for some functions):
exit
退出进程,而不是函数。您想要返回
。exit
exits the process, not the function. You wantreturn
.