Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to define a boolean data type in C? 4

Status
Not open for further replies.

sirbu

Programmer
Sep 15, 1999
15
0
0
RO
Please can anyone help with suggestions about how to define a Boolean data type in C?
For my logical tests now I use one integer and if the condition is TRUE the integer takes value 1
and if FALSE the integer gets 0. But that's rather complicated.
If anyone has a simpler way please let me know.
Thanks in advance

PS. If anyone can give me an implementation of an algorithm for making quadrangle mesh
for an 2D finite element analysis, or even some materials describing the algorithm
would help me a lot !
 
You can use enumerated data types like the following :
enum BOOLEAN{No, Yes}
This way a constant value is assigned to "Yes" ie 1 and to "No" 0(zero).
Refer to P-39 of "The C Prgmng Lang" by Dennis Ritchie/Kernighan.
Also the concept of bit-field can also be used if "storage space is at premium".
A bit-field is a set of adjacent bits in a single storage unit ie word.
Its declaration is like a structure.
struct{
unsigned int is_yes : 1;
unsigned int is_no : 1;
}result;
The number following the colon specifies the field width in bits.
To turn the bits on :
result.is_yes = 1;
off :
result.is_no = 0;
Probably using bit fields can render the program as not portable. Check this out.
For this topic also K & R can be referred at P-147.
Check out if this helps.
And could not get ur quadrangle mesh's problem exactly.
Give in more details.
 
In C there is specific data type for boolean datae. In C++ we are having bool data type.

Yes, your choice is right, you can use symbolic constants. ie you can define two seperate Symbolic constants for true and false.

Maniraja S
 

You just have to do this:

typedef unsigned char boolean;
#define TRUE 1
#define FALSE 0

boolean check=TRUE;

if (check)
{
}

like that...

unsigned char is the best way to define a boolean in terms
of FALSE/TRUE being 0/1 because there is no confusion over
negative values.

cheers
Anand :cool:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
I believe C++ rezerved words are better. In borlandC++ is boolean and in MSC++ is bool. The words true/false are supported by both compillers. John Fill
1c.bmp


ivfmd@mail.md
 
Since the new C standard, C99, was ratified, there is a new boolean data type, _Bool, which is only capable of holding the values 0 and 1. If you include the header <stdbool.h>, you bring into scope a macro, bool, which expands to _Bool.

So, you can either not include <stdbool.h> and declare boolean data types like this:

_Bool my_bool;

Or:

#include <stdbool.h>

/* ... */

bool my_bool;

It think the idea of having _Bool and requiring the <stdbool.h> header if the programmer wants to use bool is because there's a whole lot of existing code that uses bool in typedefs like this:

typedef unsigned char bool; /* or int or whatever */

And using &quot;bool&quot; as a new data type would break all that code.

Since there are no compilers claiming C99 conformance yet, your compiler may or may not support this. The <stdbool.h> header also brings the constants true and false into scope which are equal to 1 and 0 respectively.

As has been said above, you can define your own constants, but be careful! Particularly if you want your constants to be equivalent to the way C regards true and false: all non-zero values are true (not just 1) and a zero value is false. The problem is, you can't do it with defines.

Say, you defined them as abp suggested:

#define TRUE 1
#define FALSE 0

Then later in your code, you'll be making comparisons with your TRUE, FALSE types like this:

unsigned char a;

/* put something in a */

if (a==TRUE) {
/* it is true! */

But, unless a==1, the test will fail, giving unexpected results if a is some other non-zero value which is supposed to indicate &quot;true&quot;.

Even this doesn't work:

#define FALSE 0
#define TRUE !FALSE

Still, TRUE evaluates to 1.

With the new _Bool type, if you assign a non-zero value to it, the value 1 will actually get assigned to the variable. You can do stuff like this with no problem:

#include <stdbool.h>

/* ... */

bool foo=5; /* foo actually gets assigned 1 */

if (foo==true) {
/* ok! */

One pre-C99 way is with parameterized macros:

#define FALSE(n) (n)==0
#define TRUE(n) (n)!=0

Then in your tests:

if (TRUE(a)) {
/* it's true */

Personally, I think this is all a waste of time (I'm not even sure if I'll use the C99 bool type). I much prefer this:

unsigned char a;

/* ... */

if (a) {
/* it's true */

/* ... */

if (!a) {
/* it's false */
Russ
bobbitts@hotmail.com
 
by the way, GCC/G++ cpp compillers support reserved word bool. John Fill
1c.bmp


ivfmd@mail.md
 
I hope that everyone has read the question well. He
has asked,

&quot;Please can anyone help with suggestions about how to define a Boolean data type in C?&quot;

I take it as ANSI C/ANSI C with K&E extensions and not C with extensions/C++.

For me unsigned char works pretty well.
Actually i use it for portability since i need to compile
my code in platforms where C++ compilers are not supported.

Regards,
Anand
:cool:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
LOL adp,Many people use C style programming in C++. It is much easier than using pure (I'd say poor) C. I did't say what he/she/you must do it. It is up to he/she/it what to choose, pure C orC/C++ or pure C++. John Fill
1c.bmp


ivfmd@mail.md
 
Nevertheless, I think it's fair to assume that anyone asking for help in the C forum is looking for a C answer, unless they say otherwise. Offering C++ answers is only likely to confuse the issue, particularly for someone that's new to programming or that's not familiar with C++.

Also, I think it's silly to characterize C as &quot;poor&quot; without considering several other issues, such as the programmer's knowledge, the project specs, the tools available etc. Indeed, C is quite often the most appropriate tool for a given job (as is C++).
Russ
bobbitts@hotmail.com
 
no c programs support bool when it is not explicitly defined. To use bool you must make C++ programs with .C (c in upper case) or .cpp exetnsion.

P.S. Someone sent to me a mail. If you have a question please put it in the forum. I accept mails, but what is the forum for?
P.S 1
A programmer must have too dippy thoughts to say what he need pure C or pure assembler and if don't all will crash and ......... But the most people use C style programming in CPP files with many CPP instructions and operators. For example is much simplyer to use new tham calloc/malloc. In other hand, &quot;how to define a bool in c&quot; is a beginners question. Do you think, he/she/it has so big arguments to use pure C? Does he/she making drivers, operating systems or a BIOS module? Don't be ridiculous. John Fill
1c.bmp


ivfmd@mail.md
 
On the first point, ALL C compilers will soon provide the bool data type. C99 is being implemented by compiler vendors as we speak.

On the second point: You're making too many assumptions. What about the student (yes, there a lot of students that ask questions in help forums) who is taking a C class? In fact, I'm willing to bet that most of the beginners that are asking questions in this forum are also students. Surely their instructors won't be very receptive to your questionable arguments and their C programs littered with C++ constructs, which are no longer C programs at this point. Also, many programmers are in the position where C is the only option because they are writing code that must be portable to a number of different compilers and OS's and their manager will not accept anything but &quot;pure C&quot;. Or how about embedded systems programmers? Yes, C++ is used here as well, but C is used much more often. There are several microprocessors out there that don't even have a C++ compiler that is targeted at them. These are just a few of the possibilities that run counter to your reasoning.

In short, I think you're being ridiculous by assuming so much and over-generalizing. Notice that there's no ++ at the end of the C in this forum.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top