I am coming from the C++ student world and must do a simple program in C. I have to make a Boolean Flag. Is there a Boolean type in C or do I have to typedef it? I did a search but came up with no awnsers and my SAMS book does not even mention it.
I am coming from the C++ student world and must do a simple program in C. I have to make a Boolean Flag. Is there a Boolean type in C or do I have to typedef it? I did a search but came up with no awnsers and my SAMS book does not even mention it.
This might work. You can use bitfields in C to declare how many bits you want a variable to hold. For example:
struct boolean
{
unsigned boolean :1;
};
typedef struct boolean BOOLEAN
creates a variable that holds 1 bit or 2 values. These values can be yes or no if you use them like this.
#define YES 1
#define NO 0
boolean=NO;
I don't know for sure though. Its late. Just a suggestion. Good luck.
You also actually have to declare a variable of your type. Like this:
BOOLEAN boolean;
IN MSVC 5 and greater there is bool type
If not
#define BOOL int
#define TRUE 1
#define FALSE 0
#define INVALID -1
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
For Borland users:
bool bVar;
bVar=true;
if(bVar==true) bVar=false;
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
This is the easiest way I have found to make a boolean type in C:
After you've typedef'ed it, you can use it in your program where ever you want to use a boolean, without having to remember to pass 1 for true and 0 for false (it already knows this since its enumerated).Code:typedef int boolean; enum boolean {false, true};
Worth a shot, always works for me (in UNIX)
DrakkenKorin
Get off my Intarweb!!!!
Thanks!!!
I just always use:
Works for me. I don't need a "boolean" type. Just a flexible int.Code:int yesno; if (/*** CODE ***/) yesno = 0; else yesno = 1;
--Garfield
1978 Silver Anniversary Corvette
Using bitwise operators you can store up to 8 boolean expressions in one char.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Yeah, that is good for optimization. Each bit is a switch.
--Garfield
1978 Silver Anniversary Corvette
Hi!
There isnīt any boolean data-type in C.
klausi
When I close my eyes nobody can see me...
Not? Well, you can still use the method I mentioned in my reply before this one...
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
This is one of the reasons you create a header file of all the types you have created. You can include it in every project you write.
=== custTypes.h ===============
#ifndef __custTypes__
#define __custTypes__
#ifndef boolean
typedef int boolean;
enum boolean {false, true};
#endif boolean
#endif __custTypes__
=== custTypes.h ===============
enjoy.
C99 supports the use of _Bool and also bool (if stdbool.h is included), although hardly any existing compilers will allow it.There isnīt any boolean data-type in C
zen
Yes, but when I talk about something like that I mostly talk about the ANSI-standard.
klausi
When I close my eyes nobody can see me...