Thread: problem with macros

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    problem with macros

    hi,
    I can't figure out how the answer came as "11" ,according to me it should be "36".

    here is the code :

    Code:
    #include <stdio.h>
    
    
    #define PRODUCT(x)(x*x)
    main( )
    {
    int i = 5, j ;
    j = PRODUCT(i+1 ) ;
    printf ( "\n%d", j ) ;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do the macro expansion, i.e.,
    Code:
    j = PRODUCT(i+1 ) ;
    becomes:
    Code:
    j = i+1 * i+1;
    which is equivalent to:
    Code:
    j = i + (1 * i) + 1;
    To avoid such problems we generally add "extra" parentheses, e.g.,
    Code:
    #define PRODUCT(x) ((x) * (x))
    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

  3. #3
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Thank you for the detailed explanation and quick reply ...i was stuck finding the error.

  4. #4
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    hey i have one more problem i am stuck at how to expand this one ?
    according to me answer should be 9 ,16 as ++i and i++ are expanded as i = i+1. but ++i expands before printf and i++ after printf.

    Code:
    #define PRODUCT(x) ( x * x )
    main( )
    {
    int i = 3, j, k ;
    j = PRODUCT( i++ ) ;
    k = PRODUCT ( ++i ) ;
    printf ( "\n%d %d", j, k ) ;
    }
    Last edited by coder1; 09-10-2013 at 03:32 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to an earlier thread: cube program.
    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

  6. #6
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    Although i get it how j gets 9 as answer as i is incremented after the expression is over and in this situation the expression continues till the 2nd i value get multiplied, after that only the incrementation in both i happens. sop this gives us 3*3 9 as answer.
    Now in k the value of i reached is 5 and incrementation occurs before the execution of expression thus the value become 6 for i and then 7 for other ++i.
    now answer should be 6*7 = 42 but it is 49 i don't get this part .

    Plus I read the FAQs and what i can understand that ++i and i++ used more than once in a same expression has an undefined behavior and use of it should be avoided? did i get it right?
    Last edited by coder1; 09-10-2013 at 05:28 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Undefined behaviour is generally a thing to be avoided, yes.

    It's not so much the macro that is causing undefined behaviour, as it is that its expansion (with parameter i++ or ++i) results in modifying a variable twice, and the sequence of those two modifications is not defined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yes - you must avoid using the increment opertor multiple times in the same expression. The would yield all sort of problem as you have already seen one. In all fairness, I consider it as not big problem as far as you can work out how you managed to derive such an answer. The reason for you seeing 49 is quite straight forward.

    To solve the puzzle - think about it this way, a bit more logical way. The macro you have is nothing more than times the given number. Therefore, if you have 49 the only way to get 49 is by this 7 * 7. Now try workout, is there a way from you code that your can be incremented to 7.

    Code:
    i = 3;
    j = PRODUCT( i++ );    ===>  i = 3
    k = PRODUCT( ++i );   ===>   PRODUCT( ++i ) ==> ( ++i * ++i )
    
    /* In total you have ++i about three times. Which is basically 4 + 3 = 7. 
    Which is what the final expression get evaluted on. But as you can how and in 
    what sequence the complier call those incremental is undefined making it harder 
    to evaluate manually. Hence therefor must avoid using such expression */
    I'm sure others could may have explained it much better. But it is harder to put it in words.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by coder1 View Post
    hi,
    I can't figure out how the answer came as "11" ,according to me it should be "36".

    here is the code :

    Code:
    #include <stdio.h>
    
    
    #define PRODUCT(x)(x*x)
    main( )
    {
    int i = 5, j ;
    j = PRODUCT(i+1 ) ;
    printf ( "\n%d", j ) ;
    }
    Hi,
    I have never come across that syntax for macro definition. Can you direct me to a website from where it came from?
    Most I've seen is the basic #define VARIABLE 3

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    They are called macro function and arguments. Read this
    Macro Arguments - The C Preprocessor

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Macros problem
    By slain in forum C Programming
    Replies: 12
    Last Post: 02-01-2013, 07:49 PM
  2. Problem with macros and templates
    By Nickedname in forum C++ Programming
    Replies: 14
    Last Post: 10-26-2011, 06:52 AM
  3. problem with program number divisible by 10? using macros
    By MarjunC in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 05:43 AM
  4. Problem with macros..
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 07-07-2009, 02:00 AM
  5. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM

Tags for this Thread