Boolean in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KiranJyothi
    New Member
    • Apr 2007
    • 45

    Boolean in C

    Hello All,

    I am writing a code in C where I have to use the Boolean data type. I am doing -

    typedef int flag;

    #define FALSE 0
    #define TRUE 1

    void main()
    {
    :
    :
    flag = !flag;
    }

    Does "flag = !flag" is equal to "flag = FALSE"? Please let me know.

    Thanks you.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    A few caveats about making your own Bool type in C.
    1. C99 has intrinsic support for a boolean type. Refer to <stdbool.h>.
    2. If you are not using C99, it might be helpful to emulate <stdbool.h> in C89 so that your software can easily port to C99 in the future.
    3. It is better to use predicate logic with boolean variables ... if (isReady) ... rather than explicit comparisons to boolean constants ... if (isReady == FALSE). This is because there is one value for false (0), but man values for true (all nonzero values). A boolean variable could compare not-equal to the particular value of TRUE, but still be considered not false.


    The exclamation mark is the logical NOT operator. It inverts the logical sense of the operand (true becomes false; false becomes true). However, as noted above, !FALSE may not compare equal to your TRUE constant unless you chose the value the compiler assigns when it evaluates a logical expression to be true. That value is guaranteed to be "1"; but if you don't want to be responsible for remembering that then do this: #define TRUE (!FALSE).

    Comment

    • KiranJyothi
      New Member
      • Apr 2007
      • 45

      #3
      Boolean in C

      Originally posted by donbock
      A few caveats about making your own Bool type in C.
      1. C99 has intrinsic support for a boolean type. Refer to <stdbool.h>.
      2. If you are not using C99, it might be helpful to emulate <stdbool.h> in C89 so that your software can easily port to C99 in the future.
      3. It is better to use predicate logic with boolean variables ... if (isReady) ... rather than explicit comparisons to boolean constants ... if (isReady == FALSE). This is because there is one value for false (0), but man values for true (all nonzero values). A boolean variable could compare not-equal to the particular value of TRUE, but still be considered not false.


      The exclamation mark is the logical NOT operator. It inverts the logical sense of the operand (true becomes false; false becomes true). However, as noted above, !FALSE may not compare equal to your TRUE constant unless you chose the value the compiler assigns when it evaluates a logical expression to be true. That value is guaranteed to be "1"; but if you don't want to be responsible for remembering that then do this: #define TRUE (!FALSE).
      Thank you for the quick reply.

      I am trying to write a function in C which returns true (1) if there are even number of nodes in a binary tree, else returns a false (0). I am attaching the code below for your refence. Please let me know if it is correct.

      #include<stdio. h>
      #include <stdbool.h>

      bool flag = true;

      int is_even_node(tr ee_ptr p)
      {

      if(p == NULL)/*if the tree does not exist*/
      flag = false;

      if(p)/*if the tree exits*/
      {
      flag = !flag;

      is_even_node(p->left);
      is_even_node(p->right);
      }

      return int (flag);
      }

      Thank you.

      Comment

      • Tassos Souris
        New Member
        • Aug 2008
        • 152

        #4
        Why don't you declare your function to return bool?
        Code:
        bool is_even_node( tree_ptr p );
        Also make the bool flag local to the function.

        And int( flag ) is C++ not C. In C to cast you do: ( int )flag.

        Comment

        • KiranJyothi
          New Member
          • Apr 2007
          • 45

          #5
          Originally posted by Tassos Souris
          Why don't you declare your function to return bool?
          Code:
          bool is_even_node( tree_ptr p );
          Also make the bool flag local to the function.

          And int( flag ) is C++ not C. In C to cast you do: ( int )flag.
          Our professor mentioned that we should return O or 1 only. But, not bool.

          Thanks.

          Comment

          • Tassos Souris
            New Member
            • Aug 2008
            • 152

            #6
            Then use just a plain int as a flag. If you must use bool then it depends on how
            the true and false values are defined as donbock said. But i suggest you use a plain integer.

            Comment

            • KiranJyothi
              New Member
              • Apr 2007
              • 45

              #7
              Originally posted by Tassos Souris
              Then use just a plain int as a flag. If you must use bool then it depends on how
              the true and false values are defined as donbock said. But i suggest you use a plain integer.

              I was not sure that if I use 'int flag', can I do '!flag'? Because if I set the flag to 0, then is '!flag = 1'? Again, when I do '!flag', will it be equal to 0? Please let me know.

              I want to set the flag to true when there are even number of nodes in a binary tree otherwise set it to false.

              Thank you.

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #8
                Try in your compiler and see.. as donbock said in his post true expressions are guarranteed to evaluate to 1 by the standard... false is 0.. so you can play with 1 and 0...

                Comment

                • KiranJyothi
                  New Member
                  • Apr 2007
                  • 45

                  #9
                  Originally posted by Tassos Souris
                  Try in your compiler and see.. as donbock said in his post true expressions are guarranteed to evaluate to 1 by the standard... false is 0.. so you can play with 1 and 0...
                  Yes. But, unfortunately I don't have any compiler on my machine. I downloaded eclipse, but not able to use it. I think I have to install some plugin (I guess) call CDT. I will try then.

                  Thanks for your help.

                  Comment

                  • Tassos Souris
                    New Member
                    • Aug 2008
                    • 152

                    #10
                    You can use a standard traversal algorithm you know.. count the total number of
                    nodes in the tree and test if the result is even. Make the traversal algorithm to increment a counter. Here is the main idea:
                    Code:
                    int is_tree_even( tree *t ){
                            int num = 0;
                            inorder_traversal( t, &num );
                            return ( is_number_even( num ) );
                    }
                    You can make is_number_even a macro.
                    For the inorder traversal see wikipedia.

                    hope i helped :)

                    Comment

                    • KiranJyothi
                      New Member
                      • Apr 2007
                      • 45

                      #11
                      Originally posted by Tassos Souris
                      You can use a standard traversal algorithm you know.. count the total number of
                      nodes in the tree and test if the result is even. Make the traversal algorithm to increment a counter. Here is the main idea:
                      Code:
                      int is_tree_even( tree *t ){
                              int num = 0;
                              inorder_traversal( t, &num );
                              return ( is_number_even( num ) );
                      }
                      You can make is_number_even a macro.
                      For the inorder traversal see wikipedia.

                      hope i helped :)
                      I will try it once I learn how to use eclipse. You are being very helpful. Thanks a lot. I have many other questions. I will be posting very frequently now :).

                      Thanks again.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        If flag is not zero, which s TRUE, then !flag is FALSE, which is 0.
                        If flag is zero, which is FALSE, then !flag is 1, which is TRUE.

                        I guess I don't see your original problem.

                        Comment

                        Working...