简体   繁体   中英

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. But it is not happening.

Could anybody explain why?

Note:

Compiler : gcc version 4.9.2 (Debian 4.9.2-10)

You are assigning a string to a type which is of _Bool type. 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.

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 ).

you must incude stdbool.h,add use link this:first_time=true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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