473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 14980
donbock
2,426 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

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

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
8
by: Metro Sauper | last post by:
Why is the default text representation for booleans in C# 'True' and 'False' whereas in xml it is 'true' and 'false'? It seems like it would make sense to have them both the same. Metro T....
2
by: Raj | last post by:
Hi, When we are sorting the DataGrid Boolean column the grid is becoming redcross. I have my own PPMIPDataGridBoolColumn class inherited from System.Windows.Forms.DataGridBoolColumn. In this...
10
by: Henri | last post by:
In java for instance there's a way to use booleans as objects and not as value types. I would like to do the same in VB.NET so that I can check if the boolean has been explicitely defined (is not...
0
by: ECathell | last post by:
Ok this may be a silly question...but here goes... I need to extend the boolean type. the reason for this is I have a base collection that has boolean properties. Then my inherited class has...
5
by: Rob Meade | last post by:
Hi all, Quick question if I may... I have a function which depending on whether it was succesful to do something or not returns True or False as a boolean. I have another function which...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
19
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.