Boolean in C
-
Good point :D Corrected
-
Yes Yes! I actually wanted an example of a C boolean using typedef keyword.
@SaintBroseph Here's an example (code) of C boolean using typedef
#include <stdio.h> // creating custom data type bool typedef enum {false, true} bool_enum; int main() { bool_enum x=false; // declaration and initialization if(x==true) // conditional statements printf("The value of x is true"); else printf("The value of x is false"); return 0; // Output: The value of x is false }
Source: [EDIT: Link removed --JKSH]
-
typedef bool boolean; int main() { boolean A = false; if (A == false) printf("MESSAGE") else printf("MESSAGE") return 0; }
if your problem is solved then please close the thread
-
comparing a boolean to true or false is redundant. Conditional expressions return a boolean (in C, 0 or not 0) so X or !X is adequate.
Also, bool is a C99 thing via <stdbool.h>. Much C legacy still exists where there is no real boolean type but instead zero or not zero logic.
-
comparing a boolean to true or false is redundant. Conditional expressions return a boolean (in C, 0 or not 0) so X or !X is adequate.
Also, bool is a C99 thing via <stdbool.h>. Much C legacy still exists where there is no real boolean type but instead zero or not zero logic.
@Kent-Dorfman said in Boolean in C:
comparing a boolean to true or false is redundant.
Not to mention wrong, generally speaking.
1
evaluates totrue
but so does-1
, so comparing againsttrue
is simply the way to break it. Enums implicitly decay to the underlying type so checking againstcondition
and!condition
is the correct way to do it, even if atypedef
is used. -
I find it much easier to understand
if something is false
when I seeif (something == false)
than when I seeif (!something)
. Especially in longer expressions it is very easy to miss a single character like!
and read the code wrong. -
I find it much easier to understand
if something is false
when I seeif (something == false)
than when I seeif (!something)
. Especially in longer expressions it is very easy to miss a single character like!
and read the code wrong. -
I find it much easier to understand
if something is false
when I seeif (something == false)
than when I seeif (!something)
. Especially in longer expressions it is very easy to miss a single character like!
and read the code wrong.are you guys aware, that
not
is a valid keyword in c++ ? -
are you guys aware, that
not
is a valid keyword in c++ ?@J-Hilk
Yup. And it's devil's-spawn! ;-) [Same forand
&or
. If I wanted to program in Python or Pascal I would have picked that.] I would never use that, as "nobody" (most people) else uses it or knows about it, so I would regard it as an anti-pattern! -
are you guys aware, that
not
is a valid keyword in c++ ?@J-Hilk said in Boolean in C:
are you guys aware, that
not
is a valid keyword in c++ ?Yes but not in all compilers :-( MSVC does not recognize it.
-
@J-Hilk said in Boolean in C:
are you guys aware, that
not
is a valid keyword in c++ ?Yes but not in all compilers :-( MSVC does not recognize it.
-
@sierdzio
Good, but are you sure? Since it is valid since C99, I would have thought that MSVC would accept those?@JonB said in Boolean in C:
@sierdzio
Good, but are you sure? Since it is valid since C99, I would have thought that MSVC would accept those?Last time I tried was last year. Clang, GCC all are 100% fine with it, MSVC was throwing errors.
I now see it's supposed to be defined in some
iso646.h
header, I never included it, perhaps that's the reason. -
@J-Hilk said in Boolean in C:
are you guys aware, that
not
is a valid keyword in c++ ?Yes but not in all compilers :-( MSVC does not recognize it.
@sierdzio said in Boolean in C:
Yes but not in all compilers :-( MSVC does not recognize it.
VS been nonconforming! 😱 Color me surprised 😉
-
@JonB said in Boolean in C:
@sierdzio
Good, but are you sure? Since it is valid since C99, I would have thought that MSVC would accept those?Last time I tried was last year. Clang, GCC all are 100% fine with it, MSVC was throwing errors.
I now see it's supposed to be defined in some
iso646.h
header, I never included it, perhaps that's the reason. -
@sierdzio
If one has to include a header file for them, makes me wonder if they are not "part of the language", just should be available if you include the header. Are they just#define
s in that file??@JonB said in Boolean in C:
@sierdzio
If one has to include a header file for them, makes me wonder if they are not "part of the language", just should be available if you include the header. Are they just#define
s in that file??yup :D
#define and && #define and_eq &= #define bitand & #define bitor | #define compl ~ #define not ! #define not_eq != #define or || #define or_eq |= #define xor ^ #define xor_eq ^=
They are not actual C++ language reserved keywords.
-
@JonB said in Boolean in C:
@sierdzio
If one has to include a header file for them, makes me wonder if they are not "part of the language", just should be available if you include the header. Are they just#define
s in that file??yup :D
#define and && #define and_eq &= #define bitand & #define bitor | #define compl ~ #define not ! #define not_eq != #define or || #define or_eq |= #define xor ^ #define xor_eq ^=
They are not actual C++ language reserved keywords.
@sierdzio
Hmmmm.... So doesgcc
have these in some header file, or does their C++ actually have them as reserved? It does have aniso646.h
file, with the#define
s, yet you said they worked for you ingcc
without you explicitly including that? Does it include it automatically or from something else? -
@sierdzio
Hmmmm.... So doesgcc
have these in some header file, or does their C++ actually have them as reserved? It does have aniso646.h
file, with the#define
s, yet you said they worked for you ingcc
without you explicitly including that? Does it include it automatically or from something else?@JonB said in Boolean in C:
worked for you in gcc without you explicitly including that? Does it include it automatically or from something else
This header was originally in the C standard library as <iso646.h>. Compatibility header, in C defines alternative operator representations which are keywords in C++. This means that in a conforming implementation, including this header has no effect.
gcc has mostly a conforming implementation, at least in this regard
-
@JonB said in Boolean in C:
worked for you in gcc without you explicitly including that? Does it include it automatically or from something else
This header was originally in the C standard library as <iso646.h>. Compatibility header, in C defines alternative operator representations which are keywords in C++. This means that in a conforming implementation, including this header has no effect.
gcc has mostly a conforming implementation, at least in this regard
@J-Hilk said in Boolean in C:
This header was originally in the C standard library as <iso646.h>.
So what file is this in, which you say is included automatically?
-
@J-Hilk said in Boolean in C:
This header was originally in the C standard library as <iso646.h>.
So what file is this in, which you say is included automatically?
@JonB said in Boolean in C:
which you say is included automatically?
My understanding is that it is NOT included automatically in conforming C++ implementations because those understand these words as keywords and don't need this header file (which is only there for compatibility reasons).
-
@J-Hilk said in Boolean in C:
This header was originally in the C standard library as <iso646.h>.
So what file is this in, which you say is included automatically?
@JonB I'm talking about the iso646.h @sierdzio mentioned
https://en.cppreference.com/w/cpp/header/ciso646
Probably the reason why MSVC doesn't have those as keywords but requires this header is, IIRC, that it doesn't have/use a dedicated c compiler for c headers and it would break legacy stuff if those were used as keywords