You are here:

C/please help me:Macro with argument

Advertisement


Question
Thanks for your prompt reply.But I am not getting why it was incremented twice because i++ is equal to i+1 .
When the C preprocessor reach the line in Question it will replace the PRODUCT(x) with (x*x) and after that it will increment it's value by one.But you are saying it will increment twice.I am not getiing why? May be i am missing somthing about preprocessor.Please tell me.

As you said:

So, the statement:
j=PRODUCT(i++);
can be re-written as:
j = i * i; i++; i++;  ------>?
S0, the value of i is 5. (since it is incremented twice in the previous expression).



Thanks
Maitri
-------------------------
Followup To
Question -
This is the code which i run on GNU C compiler
(from knitkar let us C)

#define PRODUCT(x) (x*x)
main()
{

int i=3,j,k;
j=PRODUCT(i++);
k=PRODUCT(++i);
printf("\n%d %d", j,k);
}

output of this program is 9 and 49
but i expected 9 and 25

Irony is when i define a function instead of macro i get the ans 9 and 45
but in case of macro it doesn't give this ans.
Can you tell me why?
Answer -
First of all, you must understand that Macros and functions are different.
So, they don't have to give the same o/p.

And 2nd point you have to note is that, how the arguments are getting processed.

In your program, the i++ will get processed after multiplication.
Whereas ++i will get processed before multiplication.
So, the statement:
j=PRODUCT(i++);
can be re-written as:
j = i * i; i++; i++;
S0, the value of i is 5. (since it is incremented twice in the previous expression).

And the expression:
k=PRODUCT(++i);
can be re-written as:
++i; ++i; k = i * i;
++i is processed first for both the arguments.
So, first ++i will increment i to 6 and the second ++i will increment it to 7.
So, the latest value of i is 7.
This is used for multiplication and you will get (7 * 7) which is 49.

Hope this clarifies.  

Answer
Yes, i++ means i+1 only.
But, i++ is executed twice.

Here is how it happens.
The statement:
j=PRODUCT(i++);
after preprocessing will look like:
j=(i++ * i++);

You can see clearly that, there are two post-increments here.
So, i will be incremented twice.

To see your code after the preprocessing, you can do,
gcc -E file.c
(If you have any #includes, all of the include file will be dumped to the screen and it will become difficult to locate your code. So, my suggestion is, comment out all the #includes for this.)

Hope this clarifies.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.