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
Forum Updated to NodeBB v4.3 + New Features

Boolean in C

Scheduled Pinned Locked Moved Unsolved C++ Gurus
34 Posts 11 Posters 7.6k Views 6 Watching
  • 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.
  • Kent-DorfmanK Kent-Dorfman

    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.

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on 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
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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(:^

      JonBJ J.HilkJ 2 Replies Last reply
      2
      • sierdzioS sierdzio

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        • sierdzioS sierdzio

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

          JonBJ sierdzioS Kent-DorfmanK 3 Replies Last reply
          1
          • J.HilkJ J.Hilk

            @sierdzio , @JonB

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

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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

              @sierdzio , @JonB

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

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

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on 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(:^

              JonBJ J.HilkJ 2 Replies Last reply
              1
              • sierdzioS sierdzio

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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?

                sierdzioS 1 Reply Last reply
                0
                • JonBJ JonB

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

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on 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(:^

                  JonBJ 1 Reply Last reply
                  1
                  • sierdzioS sierdzio

                    @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 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
                    • sierdzioS sierdzio

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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??

                      sierdzioS 1 Reply Last reply
                      1
                      • JonBJ JonB

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

                        sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #21

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

                        (Z(:^

                        JonBJ 1 Reply Last reply
                        1
                        • sierdzioS sierdzio

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

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #22

                          @sierdzio
                          Hmmmm.... So does gcc have these in some header file, or does their C++ actually have them as reserved? It does have an iso646.h file, with the #defines, yet you said they worked for you in gcc without you explicitly including that? Does it include it automatically or from something else?

                          J.HilkJ 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @sierdzio
                            Hmmmm.... So does gcc have these in some header file, or does their C++ actually have them as reserved? It does have an iso646.h file, with the #defines, yet you said they worked for you in gcc without you explicitly including that? Does it include it automatically or from something else?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #23

                            @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


                            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.

                            JonBJ 1 Reply Last reply
                            2
                            • J.HilkJ J.Hilk

                              @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

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #24

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

                              jsulmJ J.HilkJ 2 Replies Last reply
                              0
                              • JonBJ JonB

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

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #25

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

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                JonBJ 1 Reply Last reply
                                2
                                • JonBJ JonB

                                  @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.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #26

                                  @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


                                  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
                                  • jsulmJ jsulm

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

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #27

                                    @jsulm
                                    Mine too. But I quote from @sierdzio above:

                                    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.

                                    My question is (should be) aimed at him: he says it worked automatically in GCC/Clang (but not MSVC) with no #include from him, that's what I'm trying to understand.

                                    1 Reply Last reply
                                    0
                                    • fcarneyF Offline
                                      fcarneyF Offline
                                      fcarney
                                      wrote on last edited by
                                      #28

                                      I know Boolean is someone's name.
                                      But it kind of look like a diet fad to scare people skinny: boo-lean.

                                      C++ is a perfectly valid school of magic.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • fcarneyF fcarney

                                        I know Boolean is someone's name.
                                        But it kind of look like a diet fad to scare people skinny: boo-lean.

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #29

                                        @fcarney You mean Anne Boleyn, one of the wifes of king Henry VI which was executed. She was mother of Elisabeth I :-)

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        jeremy_kJ 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @fcarney You mean Anne Boleyn, one of the wifes of king Henry VI which was executed. She was mother of Elisabeth I :-)

                                          jeremy_kJ Offline
                                          jeremy_kJ Offline
                                          jeremy_k
                                          wrote on last edited by
                                          #30

                                          @jsulm said in Boolean in C:

                                          @fcarney You mean Anne Boleyn, one of the wifes of king Henry VI which was executed. She was mother of Elisabeth I :-)

                                          Ha! I'm not sure what a Boleyn variable would be. Dangerous in some manner.

                                          Presumably @fcarney is referring to George Boole.

                                          Asking a question about code? http://eel.is/iso-c++/testcase/

                                          Kent-DorfmanK 1 Reply Last reply
                                          1

                                          • Login

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