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. Negativ number as true in C++.

Negativ number as true in C++.

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 6 Posters 4.8k Views 1 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.
  • B Offline
    B Offline
    broadpeak
    wrote on last edited by
    #1

    Any non-zero number (positiv AND negativ number) is true in the test-condition of the "if" statement.
    Where can I find the declaration of this rule in the c++ standard?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Search for "Boolean conversions" in the standard document, how did you expected to work?

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Hi broadpeek,

        it is very easy: an if clause checks a bool value, and bool means: false = 0.
        But I can't tell you whether it is written somewhere in the standard. If it is, it's the C-standard.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          Till now, I have known that any positiv integer is true.
          And any non positiv integer and the zero are false.

          Yeasterday I programmed a condition and I was surprised that negative integer is true too...

          Therefore, I tried to search this definition in the C++ standard but I have found nothing about this...

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on last edited by
            #5

            Quote from C++ 2003 revision:

            bq. 4.12 Boolean conversions [conv.bool]
            ... A zero value, null pointer value, or null member pointer value is converted to false; any
            other value is converted to true.

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              [quote author="Zlatomir" date="1360325618"]Quote from C++ 2003 revision:

              bq. 4.12 Boolean conversions [conv.bool]
              ... A zero value, null pointer value, or null member pointer value is converted to false; any
              other value is converted to true.[/quote]

              Yeeeeeeessssss!
              This was what I have searched :)

              Thank you very much!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Serenity
                wrote on last edited by
                #7

                [quote author="broadpeak" date="1360324319"]Till now, I have known that any positiv integer is true.
                And any non positiv integer and the zero are false.
                [/quote]

                Think more in "binary" oder "hexadeciamal" mode ;)
                There are no difference between positiv or negativ, because you cannot set a minus before a hexadecimal value :)

                for an if clause, the "if" only see the binary or hexadecimal value.
                Like
                [code] if(0x80) { ...
                [/code]

                That could be a signed (-1 I think...) or unsigned char (127), but for the if clause, it is true :)

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  broadpeak
                  wrote on last edited by
                  #8

                  [quote author="Serenity" date="1360326276"]
                  [quote author="broadpeak" date="1360324319"]Till now, I have known that any positiv integer is true.
                  And any non positiv integer and the zero are false.
                  [/quote]

                  Think more in "binary" oder "hexadeciamal" mode ;)
                  There are no difference between positiv or negativ, because you cannot set a minus before a hexadecimal value :)

                  for an if clause, the "if" only see the binary or hexadecimal value.
                  Like
                  [code] if(0x80) { ...
                  [/code]

                  That could be a signed (-1 I think...) or unsigned char (127), but for the if clause, it is true :)

                  [/quote]

                  Ok, I understand. Thanks!

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DFileccia
                    wrote on last edited by
                    #9

                    The explanations of this are very good. As far back as I can remember expressions evaluate as "true" when they are non-zero. In the *nix world, most functions returned 0 for success and negative for the error condition. M$ complicates this even more by making tokens to denote the system, error code, etc. Hence the need for the macro SUCCESS in a lot of their return values.

                    It is always a good practice, for readability and no ambiguity, to just put:

                    if (value == 0) or
                    if (value != 0)

                    and just let the compiler optimize it out.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      [quote author="Serenity" date="1360326276"]
                      Think more in "binary" oder "hexadeciamal" mode ;)
                      There are no difference between positiv or negativ, because you cannot set a minus before a hexadecimal value :)[/quote]
                      Well... you can actually :) Hex is just a notation, it doesn't change the underlying bit meaning which can signify a negative integer. It's ok to do @int a = -0x1;@ , it's just not the typical use of it.

                      This is a historic optimization. CPUs have a special instruction for checking 0 values (like jnz in assembly), so it was (no longer true of course for quite some time) cheaper to treat "anything but zero" as true.

                      1 Reply Last reply
                      0

                      • Login

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