472,131 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

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.
Sep 15 '09 #1
11 14710
donbock
2,425 Expert 2GB
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).
Sep 15 '09 #2
@donbock
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(tree_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.
Sep 15 '09 #3
Tassos Souris
152 100+
Why don't you declare your function to return bool?
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 15 '09 #4
@Tassos Souris
Our professor mentioned that we should return O or 1 only. But, not bool.

Thanks.
Sep 16 '09 #5
Tassos Souris
152 100+
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.
Sep 16 '09 #6
@Tassos Souris

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.
Sep 16 '09 #7
Tassos Souris
152 100+
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...
Sep 16 '09 #8
@Tassos Souris
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.
Sep 16 '09 #9
Tassos Souris
152 100+
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:
Expand|Select|Wrap|Line Numbers
  1. int is_tree_even( tree *t ){
  2.         int num = 0;
  3.         inorder_traversal( t, &num );
  4.         return ( is_number_even( num ) );
  5. }
  6.  
You can make is_number_even a macro.
For the inorder traversal see wikipedia.

hope i helped :)
Sep 16 '09 #10
@Tassos Souris
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.
Sep 16 '09 #11
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 16 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by greg | last post: by
8 posts views Thread by Metro Sauper | last post: by
2 posts views Thread by Raj | last post: by
reply views Thread by ECathell | last post: by
5 posts views Thread by Rob Meade | last post: by
10 posts views Thread by dba123 | last post: by
16 posts views Thread by Shawnk | last post: by
19 posts views Thread by tshad | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.