473,396 Members | 1,864 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,396 software developers and data experts.

about Boolean

how to define Boolean in C ?

boolean abc; ???

is it default as True or False??



Nov 13 '05 #1
8 43950
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**********@hong-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********@hotmail.com> ¦b¶l¥ó
news:zt********************@comcast.com ¤¤¼¶¼g...
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**********@hong-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**********@news.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********@hotmail.com> ¦b¶l¥ó
news:zt********************@comcast.com ¤¤¼¶¼g...
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**********@news.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********@hotmail.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**********@hong-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_Keith) 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********@hotmail.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
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...
22
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...
12
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...
1
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...
90
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
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
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...
8
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...
3
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; ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.