Results 1 to 8 of 8

Thread: c program

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Answers
    1

    c program

    int z, x = 5, y = -10, a = 4, b = 2;
    z = x++ - --y * b / a; what number will z in the sample code above contain?
    a) 5
    b) 6
    c) 10
    d) 11
    e) 12


  2. #2
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: c program

    Remember the following:

    • The expression x++ evaluates to the current value of x, which is 5;
    • The expression --y evaluates to the current value of y - 1, which is -11;
    • The expression --y * b / a will be evaluated as (--y * b) / a since multiplication and division have the same precedence, so they are applied left to right;
    • Since y, b, and a are all integers, the result of the division will be an integer value


    Given all that, the answer should be obvious.


  3. #3
    Junior Member
    Join Date
    May 2011
    Answers
    1

    Re: c program

    So the result will be 5 - (-11) * 2 / 4 = 10.5 ans assuming that the value is integer we get 10


  4. #4
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: c program

    Quote Originally Posted by rumax View Post
    So the result will be 5 - (-11) * 2 / 4 = 10.5 ans assuming that the value is integer we get 10
    Basically, it will evaluate as
    Code:
       5 - ((-11 * 2) / 4)
    == 5 - (-22       / 4)
    == 5 - (-5           ) 
    == 10



  5. #5
    Junior Member
    Join Date
    Apr 2011
    Answers
    13

    Re: c program

    ans will be 10


  6. #6
    Junior Member
    Join Date
    Jun 2011
    Answers
    2

    Re: c program

    precedence of Unary operators are higher than Arithmatic operators.
    x++ and --y are unary operators so these are evaluated first
    then multiplication is performed ,
    then division is performed ,
    then subtraction is performed.
    So the output is 10


  7. #7

    Re: c program

    Nice Question....................

    I tryed But i do not know it is correct or not


    first priority gives to / after * +-from lerft to right

    z=5++ + --10 * 2/4
    =5++ + -11 *1/2
    5++-5

    so answer is zero
    i tryed my level best

    Good Question


  8. #8

    Re: c program

    I have some doubt on your answer

    first Precedence is for / na from left to right
    so first 2/4 operation will perform

    check it once
    good work


  9.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact