Thread: give me an explanation

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    give me an explanation

    Code:
    #include<stdio.h>
     #include<conio.h>
    #define PRODUCT(x) (x*X)
     int main()
     {
      int i=3;
      j=PRODUCT(i+1);
      printf("%d",j);
      getch();
      return 0;
    }

    tell me why that output is comiing.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post code that you actually compiled. Copy and paste from your text editor if you need to. Furthermore, you should show what output you got, and venture your own explanation why that is so. (Hint: perform the macro expansion by hand.)
    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
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    what i get as output is 7 and this is the code i compiled.
    i am not able to understand any logic behind it,as we are giving a+1 in macro which should give 16 as output but here output is 7

  4. #4
    spaghetticode
    Guest
    The preprocessor simply exchanges text. It knows nothing about C or C++ syntax. You parameter for the macro is 3+1, so the preprocessor turns that into 3+1*3+1. Since multiplication has a higher priority as addition, the result is 3+3+1, which equals 7. If you want the result to be 16, you have to add some extra brackets in the macro: #define PRODUCT(x) ((x)*(x)).

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually his macro has a typo, so that's not even going to compile:
    Code:
    #define PRODUCT(x) (x*X)
    So it really expands to:
    Code:
    i + 1 * X

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shivam99aa
    what i get as output is 7 and this is the code i compiled.
    That is not possible, unless you have a compiler that is broken:
    • Your macro uses an identifier that does not exist at all.
    • j is used without being declared.
    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

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Code:
    #define PRODUCT(X) (x*x)
    void main()
    {
      int i=3,j,k;
    j=PRODUCT(i++);
    k=PRODUCT(++i);
    printf("%d%d",j,k);
    }
    what about this code as output given by it is 9 49 what about this...

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    i forgot to declare j while submitting the code but i declared it on mine compiler

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shivam99aa View Post
    Code:
    #define PRODUCT(X) (x*x)
    void main()
    {
      int i=3,j,k;
    j=PRODUCT(i++);
    k=PRODUCT(++i);
    printf("%d%d",j,k);
    }
    what about this code as output given by it is 9 49 what about this...
    Line 1 ... you do understand that C is case sensitive don't you?

  10. #10
    spaghetticode
    Guest
    You have already been given the best possible answer by laserlight. Take a sheet of paper and a pen and perform each step of the preprocessor and the compiler by hand. You will easily find the solution that way, but take care that you really perform step after step as the preprocessor will, and that you don't leave any step out just because you *think* you know what happens.

    EDIT: Though I'm not sure if you can really count on the way your compiler treats the increment in this case... might as well be specific to your compiler.
    Last edited by spaghetticode; 10-16-2011 at 02:46 AM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And once you've done the macro expansion, read this:
    c - Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...) - Stack Overflow

    Edit: Sorry, didn't look at the post date.
    Last edited by CornedBee; 12-21-2012 at 08:13 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Why is this old fossil being dug up?
    I know someone pressed "report post", but still
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how here j is printing 7.give the explanation
    By sunil17 in forum C Programming
    Replies: 4
    Last Post: 09-03-2010, 09:36 AM
  2. Explanation pls
    By dpp in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2009, 02:08 PM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 01:41 PM
  5. Replies: 1
    Last Post: 02-11-2002, 12:00 PM