473,581 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about Boolean

how to define Boolean in C ?

boolean abc; ???

is it default as True or False??



Nov 13 '05 #1
8 43959
typedef unsigned int boolean;

#define false 0
#define true (!false)

if the variable is in a global scope, it will be initialized to 0, i.e.
false.

if it is an auto, i.e. on the stack, the result will be undefined.

e.g.

typedef unsigned int boolean;

#define false 0
#define true (!false)

boolean fGlobal; // automatically initialized to 0, i.e. false

int main(void)
{
boolean fLocal; // uninitialized. could be 0 or non zero
}

In C, 0 is considered to be false and anything nonzero is considered true.

Hope that helps

"FrancisC" <fr**********@h ong-kong.crosswinds .net> wrote in message
news:bm******** **@news.hgc.com .hk...
how to define Boolean in C ?

boolean abc; ???

is it default as True or False??




Nov 13 '05 #2
thx a lot!

"NumLockOff " <nu********@hot mail.com> 在郵件
news:zt******** ************@co mcast.com 中撰寫...
typedef unsigned int boolean;

#define false 0
#define true (!false)

if the variable is in a global scope, it will be initialized to 0, i.e.
false.

if it is an auto, i.e. on the stack, the result will be undefined.

e.g.

typedef unsigned int boolean;

#define false 0
#define true (!false)

boolean fGlobal; // automatically initialized to 0, i.e. false

int main(void)
{
boolean fLocal; // uninitialized. could be 0 or non zero
}

In C, 0 is considered to be false and anything nonzero is considered true.

Hope that helps

"FrancisC" <fr**********@h ong-kong.crosswinds .net> wrote in message
news:bm******** **@news.hgc.com .hk...
how to define Boolean in C ?

boolean abc; ???

is it default as True or False??





Nov 13 '05 #3
Greetings.

In article <bm**********@n ews.hgc.com.hk> , FrancisC wrote:
how to define Boolean in C ?

boolean abc; ???
#include <stdbool.h>

....

_Bool abc;
is it default as True or False??


Neither. Local variables are uninitialized by default, and undefined
behaviour can result from using them before a value is assigned. Global
variables of type _Bool will be initialized to 0.

Note that this answer applies to the latest version of C, C99, which may or
may not be supported by your compiler. For C89, you'll need to use some
integer type.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4
FrancisC wrote:

thx a lot!

"NumLockOff " <nu********@hot mail.com> 在郵件
news:zt******** ************@co mcast.com 中撰寫...
typedef unsigned int boolean;

#define false 0
#define true (!false) In C, 0 is considered to be false
and anything nonzero is considered true.


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.
Though, it would also work right if you consider 1 to be true,
while all other values are false, which would be unusual code.

--
pete
Nov 13 '05 #5
in comp.lang.c i read:
In article <bm**********@n ews.hgc.com.hk> , FrancisC wrote:
how to define Boolean in C ?

#include <stdbool.h> _Bool abc;


it is not necessary to include <stdbool.h> to make use of _Bool, which is a
basic type in c99 conformant compilers.

<stdbool.h> provides several macros, among which is `bool' which expands to
`_Bool', so that you can write instead:

bool abc;

--
a signature
Nov 13 '05 #6

On Fri, 10 Oct 2003, pete wrote:
"NumLockOff " <nu********@hot mail.com> [wrote:]

#define false 0
#define true (!false)


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.


Just for fun...

[DISCLAIMER: I urge everyone *NOT* to use this code. It's bad.]

#define FALSE 0
#define TRUE 0==0

Are there any common-sense constructs for which *this* set of
#defines produces crazy results? -- Note that now, even if some
brain-dead individual were ever to write

if (x == true) ...

the code would still work as if he'd written

if (x != false) ...
But remember: The above is REALLY BAD and WILL BITE YOU if you
use it, so DON'T. Just write

if (x) ...

-Arthur
Nov 13 '05 #7
"FrancisC" <fr**********@h ong-kong.crosswinds .net> writes:
how to define Boolean in C ?

boolean abc; ???

is it default as True or False??


C90 has no builtin boolean type; see section 9 of the C FAQ
at <http://www.eskimo.com/~scs/C-faq/top.html>.

For C99, the type _Bool is builtin, and the header <stdbool.h> defines
macros for "bool", "true", and "false".

There are a number of ways to define a boolean type and the boolean
true/false values in C90. Trickery like

#define false 0
#define true (!false)

is unnnecessary; something simple like

#define false 0
#define true 1

is much clearer.

Never compare a value to true or false. If you have a boolean value,
just use it as one. For example:

int ok; /* or bool ok; */
...
if (ok) /* good */
if (!ok) /* good */
if (ok == false) /* needlessly verbose */
if (ok == true) /* needlessly verbose and potentially dangerous */

If you think (ok == false) is clearer than the equivalent (!ok),
you should think that ((ok == false) == true) is even clearer.
(ok == true) is even worse. A condition is true if the value of
the expression is any non-zero value. If the value of ok happens to
be 2, (ok) and (ok == true) are different. The builtin relational
operators will always yield 0 or 1, but there are other ways to get
boolean values.

Note that if you're using the value of a pointer as a condition,
using a (strictly redundant) comparison to NULL is acceptable:

int *ptr;
...
if (ptr) /* ok */
if (ptr != NULL) /* ok */

This is a matter of style. I personally prefer to make the comparison
explicit, but plenty of good C programmers feel differently. If you're
going to be reading other people's C code, you'll need to understand
both idioms.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #8
Arthur J. O'Dwyer wrote:

On Fri, 10 Oct 2003, pete wrote:
"NumLockOff " <nu********@hot mail.com> [wrote:]
>
> #define false 0
> #define true (!false)


For tests,
(x == false) and (x != false) will work for any integer x.

(x == true) and (x != true) will only work right
if you constrain x to be either 0 or 1.


Just for fun...

[DISCLAIMER: I urge everyone *NOT* to use this code. It's bad.]

#define FALSE 0
#define TRUE 0==0

Are there any common-sense constructs for which *this* set of
#defines produces crazy results? -- Note that now, even if some
brain-dead individual were ever to write

if (x == true) ...


if (TRUE == x) ... /* same as (1 == x) */

--
pete
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1996
by: Zunbeltz Izaola | last post by:
Hi I'm starting a new proyect and i'm in doubt about diferent interfaces for my clases. My clases will have a lot of attributes and i'm want to know what aproach could be the best 1) Define one SetAttribute/GetAttribute pair of method for each attribute. 2) Define one SetAttribute/GetAttribute which argument is a key=value format.
22
1944
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I understand best (i.e. C returns non-zero as true, and zero as false), is there anything I should be aware of given Python's different approach? ...
12
2983
by: SStory | last post by:
3rd time trying to post this but never see it in the list: =================================== In VB.NET, am doing the following with my MDI app.. Please advise if I am going about this wrong or there is a better way. I thought, well, the main form needs to be able to ask the child form if it can save
1
1368
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running 2003/IIs 6and it bitched it couldn't find the directory. I created a virtual directory, it complained because web.config wasn't there. I removed the...
90
3391
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
6
2136
by: yxq | last post by:
Hello, The File.Delete(VS2005) function can not delete file on Vista-64bit, why? And, what changes of API between 32-bit and 64-bit? Thank you
0
6685
by: godsmustbcrazy | last post by:
Here is my problem. Brand new SQL Server 2005 Installation 1. Create a database "Test" 2. Create a database user "Domain\user" and set user mapping to "Test" with datareader, datawriter permissions 3. Look at Test ->Properties->Permissions activate "Domain\user" and click effective permissions and I get this error message
8
1212
by: Tony Johansson | last post by:
Hello! I think that this text is complete enough so you can give me an explanation what they mean with it. I wonder if somebody understand what this means. It says the following : "You can't overload assignment operator, such as +=, but these operator use their simple counterpart, such as +, so you don't have to worry about that....
3
2479
by: Anil S | last post by:
Hello, my application is console application and I have created "Service Reference" of the webservices. this webservice uses username and password. my code are following: myArray nl; FeedSoapClient client = new FeedSoapClient(); client.ClientCredentials.UserName.UserName = "tellme"; client.ClientCredentials.UserName.Password = "tellme";
0
7857
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8299
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6545
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5355
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1397
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.