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. Problem in precedence of operators.

Problem in precedence of operators.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 5 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.
  • P Offline
    P Offline
    parkavikkk
    wrote on last edited by
    #1

    What number will z in the sample code given below? int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong. i am struggling in it. kindly solve it and explain. Thanks in advance. :)

    L J V 3 Replies Last reply
    0
    • P parkavikkk

      What number will z in the sample code given below? int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong. i am struggling in it. kindly solve it and explain. Thanks in advance. :)

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

      z=x++ - --y *b/a;
      x(++) - ((--y) *b / a);
      5 - (-11 * 2 / 4)
      5 - (-22 / 4)
      5 - (-5) // integer arithmetic ignores remainder
      10

      See C++ Built-in Operators, Precedence and Associativity[^].

      P 1 Reply Last reply
      0
      • P parkavikkk

        What number will z in the sample code given below? int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong. i am struggling in it. kindly solve it and explain. Thanks in advance. :)

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        See C++ Operator Precedence - cppreference.com[^]. The first step is inserting parentheses according to the predence and order:

        z = (x++) - (((--y) * b) / a);

        Then replace the variables with their values:

        z = (5++) - (((--(-10)) * 2) / 4);

        Now solve step by step but observe the special handling of the postfix operator which will return a temporary copy that contains the value before the operation (that means x will be changed but the initial value of 5 is used for the calculation). Observe also that results might get rounded wirh integer arithmetic:

        z = (5) - ((-11 * 2) / 4);
        z = 5 - (-22 / 4);
        // The remaining steps are left to your exercise

        1 Reply Last reply
        0
        • L Lost User

          z=x++ - --y *b/a;
          x(++) - ((--y) *b / a);
          5 - (-11 * 2 / 4)
          5 - (-22 / 4)
          5 - (-5) // integer arithmetic ignores remainder
          10

          See C++ Built-in Operators, Precedence and Associativity[^].

          P Offline
          P Offline
          parkavikkk
          wrote on last edited by
          #4

          thank you. i am clear now

          1 Reply Last reply
          0
          • P parkavikkk

            What number will z in the sample code given below? int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong. i am struggling in it. kindly solve it and explain. Thanks in advance. :)

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #5

            parkavikkk wrote:

            int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong.

            Of course it is wrong! Just because the integer number cannot be 2.5, nor 2.7, nor 2.8... :cool:

            J 1 Reply Last reply
            0
            • V Victor Nijegorodov

              parkavikkk wrote:

              int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a; Can anyone please solve it and explain the precedence. i am not getting right answer. :(( my answer is 2.5. but it is wrong.

              Of course it is wrong! Just because the integer number cannot be 2.5, nor 2.7, nor 2.8... :cool:

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Maybe the OP was manually calculating it?

              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