473,789 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boolean Values

How can I define a boolean value in c? (an value that can only be either
1 or 0) I feel bad for the memory loss when declaring ints for variables
that do not need that much memory.
--
Ian Tuomi
Jyväskylä, Finland

"Very funny scotty, now beam down my clothes."

GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

NOTE: Remove NOSPAM from address

Nov 13 '05
16 17545
In article <3F************ **@jpl.nasa.gov >,
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote:
Don't use the stdbool.h header file.
You have to motivate that recommendation.
If you don't have a stdbool.h header file, you can use
What should I do if I have a stdbool.h file? You just told us
not to use it.
#ifndef _STDBOOL_H
#define _STDBOOL_H 1
You are invading the implementations namespace by using a name
that starts with an underscore followed by an upper case character.
See paragraph 7.1.3 in the ANSI/ISO/IEC 9899-1999 standard.
typedef int _Bool;
Once more are you threading on someone elses property.
typedef _Bool bool
const bool false = 0;
const bool true = !false;


Using ``!false'' has no advantages over using ``1'', only disadvantages.

--
Göran Larsson http://www.mitt-eget.com/
Nov 13 '05 #11
Ian Tuomi <ia*******@co.j yu.fi> writes:
How can I define a boolean value in c? (an value that can only be either
1 or 0) I feel bad for the memory loss when declaring ints for variables
that do not need that much memory.


Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.

Don't worry too much about using up extra memory. On many systems,
using a variable smaller than a word will cost you more in extra code
size than you save in data size.

--
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 #12
Keith Thompson wrote:
Read section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>.
I looked there but it did not say how to save the memory. It only listed
the #define's for TRUE and FALSE.
Don't worry too much about using up extra memory. On many systems,
using a variable smaller than a word will cost you more in extra code
size than you save in data size.


Ok, I just felt bad for all the memory going to waste but I guess that
is irrelevant taking into consideration the current state of computers.
Thanks Everyone.

--
Ian Tuomi
Jyväskylä, Finland

"Very funny scotty, now beam down my clothes."

NOTE: Remove NOSPAM from address

Nov 13 '05 #13
On Fri, 17 Oct 2003 18:59:52 +0200, Tristan Miller
<ps********@not hingisreal.com> wrote in comp.lang.c:
Greetings.

In article <bm**********@p hys-news1.kolumbus. fi>, Ian Tuomi wrote:
How can I define a boolean value in c? (an value that can only be either
1 or 0) I feel bad for the memory loss when declaring ints for variables
that do not need that much memory.
According to the latest C standard, which your compiler may, may not, or may
only partially support:

#include <stdbool.h>

_Bool foo;


Actually, no header is required to use _Bool, which is a built-in
type. <stdbool.h> only defines the macros bool (as a synonym for
_Bool), and the values true and false.
Note that using the new boolean type doesn't guarantee that the compiler is
actually going to set aside just one bit of memory for the variable. In
fact, it probably won't. In practice, the low-level machine code
operations required to test individual bits are typically slower than those
used to compare whole words anyway. So even if you could squeeze your
boolean variables down to a single bit in size, you'd be trading off
execution speed.
Actually _Bool can't use individual bytes, it is an unsigned integer
type. You can have arrays of _Bool and take the address of a _Bool.
Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
must occupy at least as much memory as one of the character types.
If you're worried about memory and speed optimizations, concentrate on
choosing efficient algorithms. Microoptimizati ons such as you propose are
generally a waste of the programmer's time for all but the most constrained
execution environments.

Regards,
Tristan


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #14
On Fri, 17 Oct 2003 12:01:46 -0700, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote in comp.lang.c:
Ian Tuomi wrote:
How can I define a boolean value in C?
(a value that can only be either 1 or 0)
Don't use the stdbool.h header file.
If you don't have a stdbool.h header file, you can use

#ifndef _STDBOOL_H


Illegal invasion of namespace reserved for the implementation.
#define _STDBOOL_H 1
typedef int _Bool;
Guaranteed syntax error on any compiler that supports the standard C
type _Bool, because _Bool is a built-in type and a keyword. The
header <stdbool.h> is not needed to use the _Bool type on a conforming
compiler.
typedef _Bool bool
const bool false = 0;
const bool true = !false;
#endif _STDBOOL_H


Of course, in many, many ways, this does not act at all like the
standard C type _Bool.

The standard _Bool type is an unsigned type and works like this:

#include <stdbool.h> /* for macros true and false */

_Bool b1 = true;

--b1; /* b1 is now false */
--b1; /* b1 is now true */
--b1; /* b1 is now false again! */

....try that with your lame typedef.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #15
Jack Klein <ja*******@spam cop.net> writes:
[...]
Actually _Bool can't use individual bytes, it is an unsigned integer
type. You can have arrays of _Bool and take the address of a _Bool.
Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
must occupy at least as much memory as one of the character types.


Typo alert: _Bool can't use individual *bits*.

--
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 #16
In message <v0************ *************** *****@4ax.com>
Jack Klein <ja*******@spam cop.net> wrote:
Actually _Bool can't use individual bits, it is an unsigned integer
type. You can have arrays of _Bool and take the address of a _Bool.
Like any other object in C, sizeof(_Bool) must be >= 1, so a _Bool
must occupy at least as much memory as one of the character types.


That's only true when you're actually manipulating them in memory. If you had
a _Bool variable that never had its address taken, a smart
compiler would be able to store it in a single bit of memory or of a CPU
register.

And a calling standard could specify that functions returning _Bool actually
returned the result in a CPU's Zero flag, rather than in a general purpose
register - a potentially significant space and speed optimisation.

_Bool allows a number of cunning tricks like that. It just doesn't allow
arrays of bools to be packed bits, which is a shame.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #17

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

Similar topics

2
3884
by: Julian Hershel | last post by:
Hi. I want to to use the xs:boolean type for one of my elements. But is it possible to make these boolean values be read as Yes/No instead of true/false? Should I create a custom type for that? Thank you. Julian
8
43967
by: FrancisC | last post by:
how to define Boolean in C ? boolean abc; ??? is it default as True or False??
17
1739
by: jacob navia | last post by:
The section about boolean values should mention <stdbool.h> at least. It is still at the C89 stage... (Section 9)
8
3266
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. Sauper, Jr. President Sauper Associates, Inc.
0
1273
by: Robert Love | last post by:
I am trying to save some boolean values from checkboxes using isolated storage. I am able to do strings and integers without a problem but I can't work out how to save boolean values without seeing the error message below: 'Additional information: Argument 'Prompt' cannot be converted to type 'String' The error message is displayed at this line of code in the form load event.
0
1380
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 enumerated values that configure the Boolean value of the underlying class. The issue is I have a confirmation box that shows changed fields in my collection, but its showing both the enum values and the underlying Boolean value...If I make my...
10
16430
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 recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
0
1261
by: Genken | last post by:
Hi ALL I currently have a project where i have to redefine certain table fields and their data types. The problem is i have fields to mimic or duplicate an access table, but the problem is that certain fields are boolean values with yes/no attributes. Sql server does not support boolean values to my understanding. The sql server corresponding fields are of type char which is updated in a webpage by entering y or n values. Is their a way i...
6
17511
by: =?iso-8859-2?Q?Marcin_Dzi=F3bek?= | last post by:
Hi All: I need to get (filter in) some dataview's rows with DBNULLs in column of boolean type: Actually to get the only rows with DBNULL, I use code like this: DV.RowFilter = "(IsNull(MyBooleanColumnName, True) = True) AND (IsNull(MyBooleanColumnName, False) = False)" or ex. DV.RowFilter = "(IsNull(MyIntegerColumnName, 1) = 1) AND (IsNull(MyIntegerColumnName, 0) = 0)" for columns of Integer...
19
14816
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 = Convert.ToBoolean((int)dbReader)
0
9511
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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

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.