Boolean in C
-
Yes Yes! I actually wanted an example of a C boolean using typedef keyword.
@SaintBroseph
So what abouttypedef enum { false, true } mybool;if that's the sort of thing your teacher wants from you, to show you understand?
-
typedef bool boolean; -
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.
1evaluates totruebut so does-1, so comparing againsttrueis simply the way to break it. Enums implicitly decay to the underlying type so checking againstconditionand!conditionis the correct way to do it, even if atypedefis used. -
I find it much easier to understand
if something is falsewhen 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 falsewhen 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 falsewhen 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
notis a valid keyword in c++ ? -
are you guys aware, that
notis 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
notis a valid keyword in c++ ?@J-Hilk said in Boolean in C:
are you guys aware, that
notis 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
notis 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.hheader, I never included it, perhaps that's the reason. -
@J-Hilk said in Boolean in C:
are you guys aware, that
notis 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.hheader, 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#defines 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#defines 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#defines 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 doesgcchave these in some header file, or does their C++ actually have them as reserved? It does have aniso646.hfile, with the#defines, yet you said they worked for you ingccwithout you explicitly including that? Does it include it automatically or from something else? -
@sierdzio
Hmmmm.... So doesgcchave these in some header file, or does their C++ actually have them as reserved? It does have aniso646.hfile, with the#defines, yet you said they worked for you ingccwithout 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