简体   繁体   English

为什么从未达到断点?

[英]Why the breakpoint is never reached?

Consider below code: 考虑下面的代码:

#include<stdio.h>

/* Makes you guess a number and predicts it.
 */

int main(void)
{
short guess=0;
_Bool first_time="TRUE";
printf("Guess a number from 1 to 100\n");
printf("Lemme guess it\n");
printf("Press n if the guess is wrong, y otherwise\n");
    do
    {
      if (first_time)
      {
        first_time="FALSE";
      }
      else
      {
        printf("Break point"); // This point is never reached while execution
        while (getchar() != '\n')
        ; // Waste the buffer till the newline character
      }
      printf("\nMy guess : %d",++guess); 
      printf("\nAm i right ? ");

    }while( getchar() != 'y' );
return 1;
}

My instinct says the breakpoint should be reached from the second iteration of the do-while loop. 我的直觉是应该在do-while循环的第二次迭代中到达断点。 But it is not happening. 但这没有发生。

Could anybody explain why? 有人可以解释为什么吗?

Note: 注意:

Compiler : gcc version 4.9.2 (Debian 4.9.2-10) 编译器:gcc版本4.9.2(Debian 4.9.2-10)

You are assigning a string to a type which is of _Bool type. 您正在将字符串分配给_Bool类型的类型。 You probably have got warning which says that you are assigning a pointer to an integer type, or something like that. 您可能已经收到警告,提示您正在分配一个指向整数类型或类似类型的指针。 You should use 0 or 1 for _Bool type. _Bool类型应使用0或1。

If you print the value of first_time,you will found,the first_time is always 1. Please see he _Bool here( http://en.cppreference.com/w/c/language/arithmetic_types#Boolean_type ). 如果打印first_time的值,则会发现first_time始终为1。请在此处查看_Bool( http://en.cppreference.com/w/c/language/arithmetic_types#Boolean_type )。

you must incude stdbool.h,add use link this:first_time=true; 您必须包含stdbool.h,并添加使用以下链接:first_time = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM