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. why it prints 3?

why it prints 3?

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 6 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 sam_psycho

    Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?

    int a=0;
    int b=(a=0)?2:3;
    cout<

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    Because the statement within parenthesis, which assigns 0 to a, equates to 0 (false), thus assigning 3 to b. If you change it to:

    int b = (a = 1) ? 2 : 3;

    it might make more sense.

    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    CPalliniC 1 Reply Last reply
    0
    • S sam_psycho

      Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?

      int a=0;
      int b=(a=0)?2:3;
      cout<

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

      The result of the expression (assignment)

      (a=0)

      is 0, i.e. the value of a (that is false for the conditional operator ?). hence the result of the expression

      VCD_A wrote:

      (a=0)?2:3

      is 3. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • D David Crow

        Because the statement within parenthesis, which assigns 0 to a, equates to 0 (false), thus assigning 3 to b. If you change it to:

        int b = (a = 1) ? 2 : 3;

        it might make more sense.

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

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

        DavidCrow wrote:

        change it to: int b = (a = 1) ? 2 : 3; it might make more sense.

        Does it really make more sense? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        D 1 Reply Last reply
        0
        • CPalliniC CPallini

          DavidCrow wrote:

          change it to: int b = (a = 1) ? 2 : 3; it might make more sense.

          Does it really make more sense? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #5

          In that it would instead assign 2 to b, yes.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          CPalliniC 1 Reply Last reply
          0
          • D David Crow

            In that it would instead assign 2 to b, yes.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

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

            Ah, OK, it makes a lot more sense... :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • S sam_psycho

              Can anybody tell me why value of b is 3 in this code? what happens with (a=0) exactly?

              int a=0;
              int b=(a=0)?2:3;
              cout<

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #7

              a=0 is assignment. You probably wanted a==0 (two = signs), which is hte equality test. One of the things that's nice (when you're experienced), yet horrible (when you're learning) about C and C++ is that assignment is an expression, not a statement, which means that you can do what you've done there. In other languages like Ada, which class assignment as a statement, you couldn't make that mistake.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              S 1 Reply Last reply
              0
              • S Stuart Dootson

                a=0 is assignment. You probably wanted a==0 (two = signs), which is hte equality test. One of the things that's nice (when you're experienced), yet horrible (when you're learning) about C and C++ is that assignment is an expression, not a statement, which means that you can do what you've done there. In other languages like Ada, which class assignment as a statement, you couldn't make that mistake.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                S Offline
                S Offline
                sam_psycho
                wrote on last edited by
                #8

                but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?

                _ L D 3 Replies Last reply
                0
                • S sam_psycho

                  but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #9

                  In C (and C++) 0 equates to false and non-zero equates to true. The case that you mentioned is similar to

                  int a = 0;
                  if (a)
                  {
                  b = 2;
                  }
                  else
                  {
                  b = 3;
                  }

                  Since a is 0, this equates to if(0) or if(false). So the else part will be executed. But in the following snippet, the if part will be executed.

                  int a = -1;
                  if (a)
                  {
                  b = 2;
                  }
                  else
                  {
                  b = 3;
                  }

                  «_Superman_» I love work. It gives me something to do between weekends.

                  1 Reply Last reply
                  0
                  • S sam_psycho

                    but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    The result of assignment is the value that was assigned to the lvalue, not some sort of status flag indicating that the assignment succeeded. The result of a=x is x, thus a=0 equals 0.

                    1 Reply Last reply
                    0
                    • S sam_psycho

                      but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true, and a=1 does, but what is wrong with zero?

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      VCD_A wrote:

                      but if your are assigning any thing to any variable(in our case a=0), this statement suppose to a true...

                      So what would you propose the value of a and b to be in the following:

                      int a, b, c;
                      a = b = c = 5;

                      Hint: it's not 1.

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      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