Page 70 - C-Language
P. 70

#define false 0

         int main(void) {
             bool x = true;  /* Equivalent to int x = 1; */
             bool y = false; /* Equivalent to int y = 0; */
             if (x) /* Functionally equivalent to if (x != 0) or if (x != false) */
             {
                 puts("This will print!");
             }
             if (!y) /* Functionally equivalent to if (y == 0) or if (y == false) */
             {
                 puts("This will also print!");
             }
         }


        Don't introduce this in new code since the definition of these macros might clash with modern uses
        of <stdbool.h>.


        Using the Intrinsic (built-in) Type _Bool


        C99

        Added in the C standard version C99, _Bool is also a native C data type. It is capable of holding
        the values 0 (for false) and 1 (for true).


         #include <stdio.h>

         int main(void) {
             _Bool x = 1;
             _Bool y = 0;
             if(x) /* Equivalent to if (x == 1) */
             {
                 puts("This will print!");
             }
             if (!y) /* Equivalent to if (y == 0) */
             {
                 puts("This will also print!");
             }
         }


        _Bool is an integer type but has special rules for conversions from other types. The result is
        analogous to the usage of other types in if expressions. In the following


         _Bool z = X;


            •  If X has an arithmetic type (is any kind of number), z becomes 0 if X == 0. Otherwise z
              becomes 1.
            •  If X has a pointer type, z becomes 0 if X is a null pointer and 1 otherwise.

        To use nicer spellings bool, false and true you need to use <stdbool.h>.


        Integers and pointers in Boolean expressions.






        https://riptutorial.com/                                                                               46
   65   66   67   68   69   70   71   72   73   74   75