473,777 Members | 1,732 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 #1
16 17543
Ian Tuomi <ia*******@co.j yu.fi> wrote in
news:bm******** **@phys-news1.kolumbus. fi:
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.


You can't do what I think you really want in C, this will give you
something that can only hold a one or a zero:

struct MyBool
{
int boolean : 1;
};

struct MyBool flag;

flag.boolean = 1;

This still uses a full int though. If you really want to have a boolean
that uses one bit only then use an 8051 with a C compiler in non-standard
mode and declare your booleans with the 'bit' type.
--
- Mark ->
--
Nov 13 '05 #2
On Fri, 17 Oct 2003, Mark A. Odell wrote:
You can't do what I think you really want in C, this will give you
something that can only hold a one or a zero:

struct MyBool
{
int boolean : 1;
};

struct MyBool flag;

flag.boolean = 1;


Only works if the implementation treats int bitfields as unsigned.

--
au***@axis.com
Nov 13 '05 #3
Ian Tuomi <ia*******@co.j yu.fi> 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.


Unless you are writing code for a platform with extremely limited
resources you shouldn't bother at all.

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #4
Ian Tuomi <ia*******@co.j yu.fi> 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.

I generally use
typedef unsigned char bool;
enum {true=1,false=0 };
and don't worry about how tightly it's packed. I got 256Mbytes of real memory
and gigabytes of virtual.

If you have a number of booleans in one structure, you can pack them
one bit at a time,
struct {
int empty:1;
int nullable:1;
int productive:1;
int weight:7;
int leftrcr:1;
int rightrcr:1;
int embedding:1;
...
}
which can trade space for time.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I think that's kinda of personal; I don't think I should answer that.
Nov 13 '05 #5
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;

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.

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

--
_
_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 #6
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.


The problem is that in todays computers access to single bits isn't
really available. You work with entiry bytes or sometimes larger, use
and/or/xor, and compare with 0. Therefor there is no way to really do
this unless you have more than one boolean value you want to hold. If
that is the case you use bit manipulations to set and unset bits on
something like a u_char or whatever.

NR

Nov 13 '05 #7
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
#define _STDBOOL_H 1
typedef int _Bool;
typedef _Bool bool
const bool false = 0;
const bool true = !false;
#endif _STDBOOL_H

Nov 13 '05 #8
E. Robert Tisdale <E.************ **@jpl.nasa.gov > spoke thus:
Don't use the stdbool.h header file.


What? Why not?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #9
E. Robert Tisdale wrote:
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.


Do you mean "Use the stdbool.h header"?
If you don't have a stdbool.h header file, you can use

#ifndef _STDBOOL_H
#define _STDBOOL_H 1
typedef int _Bool;
typedef _Bool bool
Missing semicolon.
const bool false = 0;
const bool true = !false;
There are at least two serious problems with these definitions:

* The values of `false' and `true' cannot be used in constant
expressions.

* The header cannot be used in multiple source files. Using these
variables in more than one translation unit causes undefined
behaviour, due to multiple external definitions for `false' and
`true'.

Further, there doesn't seem to be a good reason for `true' and `false'
to be addressable.

Arguments could be made for using either an enum or macros, but const
variables are not the right way to do this. The standards people knew
what they were doing when they made `false' and `true' macros, you
know. In particular, they understood the difference between the way
`const' works in C and in C++.

Also, note that the _Bool defined here doesn't behave much like the
standard _Bool; I'd call it something else to avoid possible
confusion.
#endif _STDBOOL_H


The grammar doesn't allow tokens after #endif.

Jeremy.
Nov 13 '05 #10

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
1737
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
1272
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
17510
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
14815
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
9628
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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
10292
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
10122
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...
0
8954
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
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
3
2860
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.