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.
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.
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.)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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)).
Actually his macro has a typo, so that's not even going to compile:So it really expands to:Code:#define PRODUCT(x) (x*X)Code:i + 1 * X
Quzah.
Hope is the first step on the road to disappointment.
That is not possible, unless you have a compiler that is broken:Originally Posted by shivam99aa
- Your macro uses an identifier that does not exist at all.
- j is used without being declared.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
what about this code as output given by it is 9 49 what about this...Code:#define PRODUCT(X) (x*x) void main() { int i=3,j,k; j=PRODUCT(i++); k=PRODUCT(++i); printf("%d%d",j,k); }
i forgot to declare j while submitting the code but i declared it on mine compiler
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.
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
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.