C 退出函数没有按照我的预期执行

发布于 2024-09-26 22:40:38 字数 436 浏览 1 评论 0原文

当我使用调试器时,我可以知道退出不是退出函数。我使用退出功能错误吗? (我必须是)我该如何解决这个问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

弱骨蛰伏 2024-10-03 22:40:38

函数名称本身(后面没有括号)只是给出函数的地址,而不调用它。在 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; or 3; 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 for gcc) is a very good idea and will help you avoid some pitfalls like this.

南烟 2024-10-03 22:40:38

你必须调用它:

exit(0);

另外,如果你把它放在 return 之后,它永远不会被调用,因为 return 从函数返回。

编辑:并且,正如其他人所说, exit 退出程序,所以您可能不想在这里使用 exit 。

You must call it:

exit(0);

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.

永不分离 2024-10-03 22:40:38

除了 returnexit 的错误之外,您使用整数和字符的方式也存在错误。 isdigit 是一个仅应用于字符的函数,如果字符位于 '0''9' 之间,则返回 true,但应该知道 C 中的字符表示法只是编写代码点(大多数时候是 ASCII)的一种奇特方式。因此,当您在 C 程序中编写 '1' 时,编译器将看到 49,如果您编写 'a' 编译器实际上会看到97。这意味着 isdigit 对于值 4857 返回 true,可能不是您想要的。在将 divider'1' 进行比较的行中,实际上您是将其与 49 进行比较(IBM 大型机除外,其中 < code>'1' is 241)

你的循环是无限的,它取决于 x 的值,但 x 在循环中没有改变,所以 while 中的条件永远不会改变。

编辑:这里是更正后的代码

int is_prime(uint_t x)
{
uint_t divider;

  if(x <= 3)
    return 1;

  for(divider = x-1; ; divider--) {

    if(x % divider  == 0)
      return 0; //not prime

    if(divider == 1)
      return 1;  //if divider reaches 1 then the number is prime

  }
}

Besides the bugs of returnand exit 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 see 49, if you write 'a' the compiler sees in reality 97. This means that isdigit return true for the values 48 to 57, probably not what you intended. In the line where you compare divider with '1', in reality you're comparing it with 49 (except on IBM mainframe, where '1' is 241)

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

int is_prime(uint_t x)
{
uint_t divider;

  if(x <= 3)
    return 1;

  for(divider = x-1; ; divider--) {

    if(x % divider  == 0)
      return 0; //not prime

    if(divider == 1)
      return 1;  //if divider reaches 1 then the number is prime

  }
}
爱格式化 2024-10-03 22:40:38

阅读 exit(3) 的手册。

Read exit(3)'s manual.

好久不见√ 2024-10-03 22:40:38

该语句:

exit;

给出以下 GCC 警告:

C:\temp\test.c:71: warning: statement with no effect

发生的情况是,您有一个计算结果为 exit() 函数地址的表达式 - 但该表达式实际上并未对该地址执行任何操作。这类似于您有这样的陈述:

1 + 2;

它是有效的 C,但它对任何东西都没有影响。

要调用该函数,作为 Thomas Padron -McCarth 说,你必须有参数列表(即使对于某些函数来说它们是空的):

exit(0);

The statement:

exit;

gives the following warning with GCC:

C:\temp\test.c:71: warning: statement with no effect

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:

1 + 2;

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(0);
多情出卖 2024-10-03 22:40:38

exit 退出进程,而不是函数。您想要返回

exit exits the process, not the function. You want return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文