Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Boolean in C
QtWS25 Last Chance

Boolean in C

Scheduled Pinned Locked Moved Unsolved C++ Gurus
34 Posts 11 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SaintBroseph
    wrote on 13 Oct 2021, 07:22 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 13 Oct 2021, 07:24 last edited by
      #2

      bool is a built-in type in C, you don't have to implement anything.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SaintBroseph
        wrote on 13 Oct 2021, 07:44 last edited by
        #3

        Yes Yes! I actually wanted an example of a C boolean using typedef keyword.

        J D 2 Replies Last reply 13 Oct 2021, 07:45
        0
        • S SaintBroseph
          13 Oct 2021, 07:44

          Yes Yes! I actually wanted an example of a C boolean using typedef keyword.

          J Offline
          J Offline
          JonB
          wrote on 13 Oct 2021, 07:45 last edited by JonB
          #4

          @SaintBroseph
          So what about

          typedef enum { false, true } mybool;
          

          if that's the sort of thing your teacher wants from you, to show you understand?

          1 Reply Last reply
          1
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 13 Oct 2021, 07:45 last edited by sierdzio
            #5
            typedef bool boolean;
            

            (Z(:^

            J 1 Reply Last reply 13 Oct 2021, 07:47
            0
            • S sierdzio
              13 Oct 2021, 07:45
              typedef bool boolean;
              
              J Offline
              J Offline
              JonB
              wrote on 13 Oct 2021, 07:47 last edited by JonB
              #6

              @sierdzio

              #typedef bool boolean

              What is #typedef in C/C++? ;-)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 13 Oct 2021, 07:49 last edited by
                #7

                Good point :D Corrected

                (Z(:^

                1 Reply Last reply
                0
                • S SaintBroseph
                  13 Oct 2021, 07:44

                  Yes Yes! I actually wanted an example of a C boolean using typedef keyword.

                  D Offline
                  D Offline
                  DarkChocolateMuffinz
                  wrote on 13 Oct 2021, 08:05 last edited by JKSH
                  #8

                  @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]

                  1 Reply Last reply
                  1
                  • K Offline
                    K Offline
                    Ketan__Patel__0011
                    wrote on 23 Oct 2021, 11:25 last edited by Ketan__Patel__0011
                    #9
                    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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kent-Dorfman
                      wrote on 25 Oct 2021, 05:22 last edited by
                      #10

                      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.

                      K 1 Reply Last reply 25 Oct 2021, 13:56
                      2
                      • K Kent-Dorfman
                        25 Oct 2021, 05:22

                        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.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 25 Oct 2021, 13:56 last edited by
                        #11

                        @Kent-Dorfman said in Boolean in C:

                        comparing a boolean to true or false is redundant.

                        Not to mention wrong, generally speaking. 1 evaluates to true but so does -1, so comparing against true is simply the way to break it. Enums implicitly decay to the underlying type so checking against condition and !condition is the correct way to do it, even if a typedef is used.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          sierdzio
                          Moderators
                          wrote on 26 Oct 2021, 11:30 last edited by
                          #12

                          I find it much easier to understand if something is false when I see if (something == false) than when I see if (!something). Especially in longer expressions it is very easy to miss a single character like ! and read the code wrong.

                          (Z(:^

                          J J.HilkJ 2 Replies Last reply 26 Oct 2021, 11:37
                          2
                          • S sierdzio
                            26 Oct 2021, 11:30

                            I find it much easier to understand if something is false when I see if (something == false) than when I see if (!something). Especially in longer expressions it is very easy to miss a single character like ! and read the code wrong.

                            J Offline
                            J Offline
                            JonB
                            wrote on 26 Oct 2021, 11:37 last edited by JonB
                            #13

                            @sierdzio
                            I see a personal-choice-disagreement debate looming... ;-)

                            I do agree it is "unfortunate" that C chose just that little ! for "not". But personally I never write == false or != false, because I would never "say" that in RL....

                            1 Reply Last reply
                            0
                            • S sierdzio
                              26 Oct 2021, 11:30

                              I find it much easier to understand if something is false when I see if (something == false) than when I see if (!something). Especially in longer expressions it is very easy to miss a single character like ! and read the code wrong.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on 26 Oct 2021, 11:37 last edited by
                              #14

                              @sierdzio , @JonB

                              are you guys aware, that not is a valid keyword in c++ ?

                              https://en.cppreference.com/w/cpp/keyword/not


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              J S K 3 Replies Last reply 26 Oct 2021, 11:45
                              1
                              • J.HilkJ J.Hilk
                                26 Oct 2021, 11:37

                                @sierdzio , @JonB

                                are you guys aware, that not is a valid keyword in c++ ?

                                https://en.cppreference.com/w/cpp/keyword/not

                                J Offline
                                J Offline
                                JonB
                                wrote on 26 Oct 2021, 11:45 last edited by JonB
                                #15

                                @J-Hilk
                                Yup. And it's devil's-spawn! ;-) [Same for and & 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!

                                1 Reply Last reply
                                0
                                • J.HilkJ J.Hilk
                                  26 Oct 2021, 11:37

                                  @sierdzio , @JonB

                                  are you guys aware, that not is a valid keyword in c++ ?

                                  https://en.cppreference.com/w/cpp/keyword/not

                                  S Offline
                                  S Offline
                                  sierdzio
                                  Moderators
                                  wrote on 26 Oct 2021, 11:46 last edited by
                                  #16

                                  @J-Hilk said in Boolean in C:

                                  @sierdzio , @JonB

                                  are you guys aware, that not is a valid keyword in c++ ?

                                  https://en.cppreference.com/w/cpp/keyword/not

                                  Yes but not in all compilers :-( MSVC does not recognize it.

                                  (Z(:^

                                  J J.HilkJ 2 Replies Last reply 26 Oct 2021, 11:47
                                  1
                                  • S sierdzio
                                    26 Oct 2021, 11:46

                                    @J-Hilk said in Boolean in C:

                                    @sierdzio , @JonB

                                    are you guys aware, that not is a valid keyword in c++ ?

                                    https://en.cppreference.com/w/cpp/keyword/not

                                    Yes but not in all compilers :-( MSVC does not recognize it.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 26 Oct 2021, 11:47 last edited by
                                    #17

                                    @sierdzio
                                    Good, but are you sure? Since it is valid since C99, I would have thought that MSVC would accept those?

                                    S 1 Reply Last reply 26 Oct 2021, 11:49
                                    0
                                    • J JonB
                                      26 Oct 2021, 11:47

                                      @sierdzio
                                      Good, but are you sure? Since it is valid since C99, I would have thought that MSVC would accept those?

                                      S Offline
                                      S Offline
                                      sierdzio
                                      Moderators
                                      wrote on 26 Oct 2021, 11:49 last edited by
                                      #18

                                      @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.

                                      (Z(:^

                                      J 1 Reply Last reply 26 Oct 2021, 11:52
                                      1
                                      • S sierdzio
                                        26 Oct 2021, 11:46

                                        @J-Hilk said in Boolean in C:

                                        @sierdzio , @JonB

                                        are you guys aware, that not is a valid keyword in c++ ?

                                        https://en.cppreference.com/w/cpp/keyword/not

                                        Yes but not in all compilers :-( MSVC does not recognize it.

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on 26 Oct 2021, 11:50 last edited by
                                        #19

                                        @sierdzio said in Boolean in C:

                                        Yes but not in all compilers :-( MSVC does not recognize it.

                                        VS been nonconforming! 😱 Color me surprised 😉


                                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                        Q: What's that?
                                        A: It's blue light.
                                        Q: What does it do?
                                        A: It turns blue.

                                        1 Reply Last reply
                                        1
                                        • S sierdzio
                                          26 Oct 2021, 11:49

                                          @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 Offline
                                          J Offline
                                          JonB
                                          wrote on 26 Oct 2021, 11:52 last edited by
                                          #20

                                          @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??

                                          S 1 Reply Last reply 26 Oct 2021, 11:54
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved