Operator Precedence Problem

This is a discussion on Operator Precedence Problem within the C Programming forums, part of the General Programming Boards category; Consider the following: Code #1 #include <stdio.h> int main() { int a=1, b=2, c=3, n; n = (( ++ a ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Question Operator Precedence Problem

    Consider the following:

    Code #1

    #include <stdio.h>

    int main() {
    int a=1, b=2, c=3, n;
    n = (( ++ a ) * b ) - ( c -- );
    printf("a = %d, b = %d and c = %d\n", a, b, c);
    printf("with n = %d", n);
    return 0;
    }

    The output for Code #1 is:

    a = 2, b = 2, c = 2
    with n = 1

    I am trying to figure out exactly how this code is read and performed by the computer.

    Now, consider an earlier example that I attempted.

    Code #2

    #include <stdio.h>

    int main() {
    int i = 3, j = 8, k;
    k = j +++ i;
    k /= 2;

    printf(%d %d %d", j, i, k);
    return 0;
    }

    the output for Code #2 is:

    9 3 5

    From this I concluded that the j++ operation takes effect after the line statement k = j +++ i;

    This is because if j became 9 during the statement, then the final value of k would be 6.

    However this does not seem to match the output of Code #1, as if ++a and c-- did not take effect on the number until after the line, then the value of n would be:

    (( 1 ) * 2 ) - ( 3 ) = -1

    Furthermore, of both took effect during the line statement, then the value of n would be:

    (( 2) * 2 ) - ( 2) = 2

    I must be interpreting this wrong. Could someone please help me?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Code:
    int a=1, b=2, c=3, n;
    n = (( ++ a ) * b ) - ( c -- );
    This reads as ((2) * 2) - (3). a is incremented and evaluates to the incremented value, 2. b obviously stays the same. c is decremented, but evaluates to the value before it was decremented since you're doing c-- instead of --c.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    I just figured it out because of what you said!

    This was what I didn't know:

    i++ or i-- is incremented/decremented after an expression is evaluated
    ++i or --i is incremented/decremented before an expression is evaluated

    It all makes sense now

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    << !! Posting Code? Read this First !! >>
    If I see post #10 from you containing code without tags, I'll just delete it.
    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. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

Tags for this Thread


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