Hi All,
I want to define a variable by type boolean in C and I don't know how. Is there anyone know, please help me? Thanks a lot!
Printable View
Hi All,
I want to define a variable by type boolean in C and I don't know how. Is there anyone know, please help me? Thanks a lot!
You could us an enummeration for that.
Or, if your compiler supports the latest C-standard (C99), you can include stdbool.h and use the bool-type.Code:typedef
{
FALSE,
TRUE
} boolean;
Thanks Shiro.
I wonder when you coded like that, TRUE = 1 and FALSE = 0, right?
Code:typedef
{
FALSE,
TRUE
} boolean;
struct dataInfo {
boolean DataReady;
}
So in later in program, I put the check statement
if (DataReady)
{ .... doing someting;
DataReady = FALSE;
}
Is this OK?
Yes, FALSE == 0.
Hmmm, I made a mistake. Sorry, it should be:
Code:typedef enum
{
FALSE,
TRUE
} boolean;
>if (DataReady)
That is OK, if this DataReady is a standalone variable and not a member of dataInfo.
=> That is OK, if this DataReady is a standalone variable and not a member of dataInfo
What do you mean by saying that? Sorry for slow understanding.
I referred to this:
> struct dataInfo {
> boolean DataReady;
> }
Take a look at this:
Probably "stand-alone" is not the correct word, but I don't know the correct English word for it.Code:/* A variable of type struct dataInfo, containing DataReady. */
struct dataInfo dataInfo_variable;
/* A "stand-alone" DataReady variable. */
boolean DataReady;
/* Use DataReady as stored in the structure-variable. */
if (dataInfo_variable.DataReady == FALSE)
{
}
/* Use the "stand-alone" DataReady variable. */
if (DataReady == FALSE)
{
}
I got it. Thanks a bunch.
From the standard :Quote:
Originally posted by Shiro
Probably "stand-alone" is not the correct word, but I don't know the correct English word for it.
A real type might be the proper expression here.Quote:
The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. The integer and real floating types are collectively called real types.
just an idea:
typedef enum boolean { False, True} bool_t;
:p
Why not
Code:
#define TRUE 1
#define FALSE 0
Isn't this the simplest?
Also, C99 does support boolean type. Use it if you can.
>Isn't this the simplest?
It depends on your point of view. I personally prefer a specialized type defined as boolean (for C89) to aid in readability:
Now instead of int you declare a bool_t, I consider this to be more readable. Your mileage may vary.Code:typedef enum { BFALSE, BTRUE } bool_t;
-Prelude
I'll go with the same way as Mr.C:
This isn't the most elegant, but it works :)Code:#define TRUE 1
#define FALSE 0
In your program you can do this:
But this isn't a new type, to use the bool type, define him with typedef as people told you here.Code:bLegalVal = FALSE;
>This isn't the most elegant, but it works
Using a defined type also works, in fact it works almost the same way, but it is more readable. The constant values in the enum can be used just like the #define'd values. Look at this:
From variable b it is immediately clear that it is a boolean-variable, it is not clear from variable a. In my opinion it is good to make a program elegant and readable.Code:int a;
boolean b;
Ohh, I see now shiro, it really looks strange something like that:
It's really more readable:Code:int bCorr = FALSE
I didn't think about it :)Code:boolean bCorr = FALSE