Boolean in C
-
Hi,
Can anyone share an example of implementing a boolean data type in C using typedef keyword?
P.S: I need it for my assignment.
-
boolis a built-in type in C, you don't have to implement anything. -
Yes Yes! I actually wanted an example of a C boolean using typedef keyword.
-
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.