Thread: Why is the answer 5 to this code and not 4?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    Question Why is the answer 5 to this code and not 4?

    I am still a novice. Can someone explain this to me? Why is the answer 5 instead of 4?

    insert
    Code:
    #include<stdio.h>
    #define TWICE(x) 2*x
    main(){
    printf("%d\n", TWICE(3-1));
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    TWICE(3-1) expands to 2*3-1

    Now consider
    #define TWICE(x) 2*(x)

    TWICE(3-1) expands to 2*(3-1)

    It's important to remember that macros are simple text replacements - nothing is evaluated until the expansion happens.
    Last edited by whiteflags; 06-28-2016 at 07:20 PM.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    16
    Actually, that still evaluates to 4. Did you mean to say that it expands to 2*3-1, which is equal to 5?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Simple:
    Code:
    #define TWICE(x) 2*x
    main(){
    printf("%d\n", TWICE(3-1));
    }
    After preprocessor processing, leaving off "#include<stdio.h>"

    Code:
    main(){
    printf("%d\n", 2*3-1);
    }
    2 times 3 minus 1 is 5!

    Your code should look something like:

    Code:
    #include<stdio.h>
    
    #define TWICE(x) 2 * (x)
    
    int main(void)
    {
       printf("%d\n", TWICE(3-1));
    }
    Please note the added parentheses around the second 'x' in the #define! The 'x' in the #define is just a textual placeholder.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Even though the multiplicative operator * has a high precedence, it is not the highest possible, so it is advisable to group the entire expression using parentheses:
    Code:
    #define TWICE(x) (2 * (x))
    Otherwise, precedence problems may still be possible, e.g.,
    Code:
    #define TWICE(x) (x) * 2
    
    /* ... */
    
    printf("%d\n", (int)TWICE(2.6));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  2. plzzz!!! answer this c++proggram on how to use do,while code?
    By randyjhon143 in forum C++ Programming
    Replies: 13
    Last Post: 03-12-2009, 04:32 PM
  3. analyze the answer for the code
    By 2001beibei in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 02:06 AM
  4. Replies: 4
    Last Post: 05-06-2008, 05:15 AM
  5. Replies: 1
    Last Post: 01-23-2002, 03:34 AM

Tags for this Thread