Like Tree2Likes
  • 1 Post By Adak
  • 1 Post By Salem

List of precedence problem

This is a discussion on List of precedence problem within the C Programming forums, part of the General Programming Boards category; Hi. I have tried an arithmetic question, but I cannot get the same final answer with the computer output. Here ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    List of precedence problem

    Hi. I have tried an arithmetic question, but I cannot get the same final answer with the computer output. Here is the code.

    int p=1;
    int r=25;
    int g=2;
    int t=3;
    int k;
    k= p++ + --r/g++ - ++t%g++;


    1) g++ --> g = 2+1 = 3.
    2) t % g = 3%3 = 0
    3) ++ t%g++ --> 0 becomes 1
    4) g++ --> g = 3+1 = 4;
    5) r/g = 25/4 = 6;
    6) --r/g++ --> 6 - 1 = 5;
    7) p++ --> 1 + 1 = 2

    Then the k will be 2 + 5 - 1 = 6.However the C compiler gives me K = 13.Can anyone expert guide me where have I done wrongly? Thanks.
    Last edited by Salem; 12-09-2011 at 11:30 PM. Reason: massive cleanup - improve your presentation in future

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ericjoe View Post
    Hi. I have tried an arithmetic question, but I cannot get the same final answer with the computer output. Here is the code.
    Code:
    int p=1;    
    int r=25;    
    int g=2;    
    int t=3;    
    int k;    
    
    k= p++ + --r/g++ - ++t%g++;
    Don't have code like this. Lay out the precedence you want followed, with parentheses, and break it up over multiple lines of code.

    Think of someone wanting (perhaps you in 5 years) to extend or modify this code. Think of the difficulty of mapping out the precedence inherent in that line, and then multiply it by perhaps a hundred or a thousand lines of code.

    Writing code like that is just kicking yourself in the butt.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by Adak View Post
    Code:
    int p=1;    
    int r=25;    
    int g=2;    
    int t=3;    
    int k;    
    
    k= p++ + --r/g++ - ++t%g++;
    Don't have code like this. Lay out the precedence you want followed, with parentheses, and break it up over multiple lines of code.

    Think of someone wanting (perhaps you in 5 years) to extend or modify this code. Think of the difficulty of mapping out the precedence inherent in that line, and then multiply it by perhaps a hundred or a thousand lines of code.

    Writing code like that is just kicking yourself in the butt.

    Or somebody else in the face

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah! Hey intern, I need you to ...
    CommonTater likes this.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > k= p++ + --r/g++ - ++t%g++;
    You should either slap your "teacher" for not having a clue about C programming, or throw away your book.
    Both are wrong if they think they can assert an answer from that expression.

    Read -> Question 3.2
    Tclausex likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. precedence problem
    By onlynishant in forum C Programming
    Replies: 3
    Last Post: 06-15-2011, 12:50 PM
  2. Operator Precedence Problem
    By bthomson900 in forum C Programming
    Replies: 3
    Last Post: 11-10-2010, 10:37 PM
  3. precedence problem
    By timhxf in forum C Programming
    Replies: 3
    Last Post: 12-13-2006, 08:10 AM
  4. precedence
    By modec in forum C Programming
    Replies: 3
    Last Post: 05-22-2003, 11:37 AM
  5. precedence
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 11:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21