mardi 29 octobre 2019

_Bool loses it's value after IF/Else statement

I'm trying to understand how _Bool works in C.

Why would it lose it's value after a IF-Else statement like I did here below? It's an easy program to understand, the user inputs a number and if it's higher or equal to 0, than the statement should be true. If its lower (or a negative number) it should say that it's false.

During the IF progress, it does get a value of 1 if the user inputs 10. But then after the if statement it prints false.

  int myVariable;
  _Bool testBool;
  printf("Write a number: ");
  scanf("%d", &myVariable);

  if (myVariable >= 0) {
    printf("Your number is higher or equal to 0, which should give the Bool value TRUE (1).\n");
    _Bool testBool = true;
    printf("Bool: %i", testBool);
  }
  else {
    printf("Your number is lower than 0, which should give the Bool value FALSE (0).\n");
    _Bool testBool = false;
    printf("Bool: %i", testBool);
  }

  printf("\nBool Value outside of IF-statement: %i", testBool);

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire