Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is the behavior of a statement logically operated with 0?

what is the behavior of a statement logically operated with 0?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
6 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    shaktikanta
    wrote on last edited by
    #1

    ex- if( 0 < -1 ) I remember "C" compiler takes bits data of -1 to signed format and returns true for the above statement. Whats behavior in C++. Is the behavior same/different.Any updated standard(C/C++) brought changes to this behavior.Please answer.

    CPalliniC 1 Reply Last reply
    0
    • S shaktikanta

      ex- if( 0 < -1 ) I remember "C" compiler takes bits data of -1 to signed format and returns true for the above statement. Whats behavior in C++. Is the behavior same/different.Any updated standard(C/C++) brought changes to this behavior.Please answer.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      In C a zero value is assigned to (0 < -1) expression, hence, in the following code

      if ( 0 < -1)
      {
      k = 5;
      }
      else
      {
      k = 10;
      }

      the statement k = 10; is executed. In C++ the esame expression is evaluated as false and (like happened in the C program) the statement k = 10; is executed.

      Veni, vidi, vici.

      In testa che avete, signor di Ceprano?

      S 1 Reply Last reply
      0
      • CPalliniC CPallini

        In C a zero value is assigned to (0 < -1) expression, hence, in the following code

        if ( 0 < -1)
        {
        k = 5;
        }
        else
        {
        k = 10;
        }

        the statement k = 10; is executed. In C++ the esame expression is evaluated as false and (like happened in the C program) the statement k = 10; is executed.

        Veni, vidi, vici.

        S Offline
        S Offline
        shaktikanta
        wrote on last edited by
        #3

        I tried on gcc compiler, the statement 0 < -1 always false.please help.

        CPalliniC 1 Reply Last reply
        0
        • S shaktikanta

          I tried on gcc compiler, the statement 0 < -1 always false.please help.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Of course. I made a blunder. :( I am very sorry. Please see my (hopefully) fixed answer.

          Veni, vidi, vici.

          In testa che avete, signor di Ceprano?

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            Of course. I made a blunder. :( I am very sorry. Please see my (hopefully) fixed answer.

            Veni, vidi, vici.

            S Offline
            S Offline
            shaktikanta
            wrote on last edited by
            #5

            Oh.. fine..But I read somewhere that when a signed value is compared with unsigned, signed converted to unsigned first. 0 is by default unsigned, hence signed version of -1 is greater than 0. May be old compiler showing this behavior.Do u have any idea?

            CPalliniC 1 Reply Last reply
            0
            • S shaktikanta

              Oh.. fine..But I read somewhere that when a signed value is compared with unsigned, signed converted to unsigned first. 0 is by default unsigned, hence signed version of -1 is greater than 0. May be old compiler showing this behavior.Do u have any idea?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Oh, now I see. You are looking for an example of 'idiosyncrasy of integer promotions' (see, for instance, , however

              shaktikanta wrote:

              0 is by default unsigned

              is a wrong assumption. It is signed, by default. You have to write:

              #include <stdio.h>
              int main()
              {
              int k;
              if ( 0u < -1)
              {
              k=5;
              }
              else
              {
              k=10;
              }
              printf("%d\n", k);
              return 0;
              }

              Veni, vidi, vici.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups