order of precedence

This is a discussion on order of precedence within the C Programming forums, part of the General Programming Boards category; Hi people! I've been going through " the C puzzle book " by Alan R. Feuer and i've come across ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Smile order of precedence

    Hi people!

    I've been going through "the C puzzle book" by Alan R. Feuer
    and i've come across something that doesn't seem right to me
    but the book gives very little explantion so could somebody help me out?

    The exercise is to find the value of x from this;

    x=(7+6) % 5 / 2;

    what i dont get is that the book says to solve it like this;

    x=((7+6) % 5)/2 so x=1

    but / is above % in the order of precedence so shouldn't it be

    x=(7+6)%(5/2)

    giving a completely different answear?

    Have i just not thought this through right? Is my braining playing tricks on me?
    I control the system ....right?!?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but / is above % in the order of precedence
    / and % have the same precedence, and left to right associativity.
    My best code is written with the delete key.

  3. #3
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    woops

    but / is above % in the order of precedence
    sorry, i mean that the division will be done....errr....before the modulos so why is the answear like this?;

    x=((7+6) % 5)/2 so x=1
    I control the system ....right?!?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    As Prelude said, both / and % have the same precedence, which means that they will be executed in order of appearance from left to right.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well look at the following code. Guess why the output is 34. And definitely u will understand why x = 1

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i=10;
        
        printf("%d",(i++ + ++i)+ ++i);
        
        getchar();
        return 0;
    }
    ssharish2005

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Except that your example is undefined, due to it modifying i multiple times in the same line of code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    thanks guys!

    I think i gte it now....

    later
    I control the system ....right?!?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-29-2008, 12:26 AM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 04:45 PM
  3. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM

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