tags:

views:

106

answers:

7

Hello, I am trying some program and confused with the output of the program

#include<stdio.h>

#define a(x) (x*x) 

int main() 
{
 int i=3,j;

 j=a(i+1);
 printf("%d",j);
 return 0;
}

I want to know why the program is not giving the output 16(as instead to that i an getting the output 7 for the above program )

i understood the point very much but if the program like this

#include<stdio.h>

#define a(x) (x*x)

int main()
{
 int i=3 ,j,k;

 j=a(i++);
 k=a(++i);
 printf("%d\n%d",j,k);
 return 0;

} 

then why the above program would give output 9 and 49....

plz help

+2  A: 

Read up on C operator precedence and think about what the macro a expands to in this case.

Paul R
+5  A: 

Because you made a bad macro:

a(i + 1)

expands to

i + 1 * i + 1

which is equivalent to

i + i + 1

or

2 * i + 1

Use parenthesis:

#define a(x) ((x) * (x))

And then you'll get it to expand to

(i + 1) * (i + 1)

which does what you want.

Chris Lutz
thanks buddy ....
sandy101
can u tell me what is the problem in the new macros ....
sandy101
@sandy101: The problem with the edited code is that it changes `i` twice without an intervening sequence point. That's invoking undefined behavior. (Or is it _unspecified_ in this case? Anyway, the result could be different for different compilers, moon phases and whatnot)
sbi
@sbi Undefined, definitely. @sandy Please do not change the question into another question by editing it, suddenly all the answers become wrong or incomplete. Your second question has been beaten to death here, plus it attracts the kind of people who say that if you were a proper engineer you should be able to predict what undefined constructs do. Please, so a StackOverflow search.
Pascal Cuoq
A: 

Another write-up for the explanation is at http://en.wikipedia.org/wiki/C_preprocessor#Precedence

Pascal Cuoq
+2  A: 

After preprocessing the line

j=a(i+1);

will be:

j=(i+1*i+1);

which when evaluated for i=3 will give j=7:

j=(3+1*3+1);

To get the desired result you need to define the macro as:

#define a(x) ((x)*(x)) 

which results in:

j=((i+1)*(i+1));

and gives the result 16 when i=3

codaddict
can u tell me the problem with the new macros
sandy101
+1  A: 

Because a(i+1) gets preprocessed into (i+1*i+1).

And 3+3+1 = 7.

You might want to use parenthesis around x.

edit: Wow, is this redundant or what. :/

Michael Foukarakis
A: 

Because your macro is wrong. Apparently it works, but the error is more subtle (not quite, but still), as the expanded code has some issues, by not following the expected order of operations.

j = a(i+1) will expand to j = i + 1 * i + 1 which is 7.

If you want to resolve your problem redefine your macro as:

#define a(x) ((x)*(x))

It's good that you've encountered this problem now, and not later. Those type of errors, are sometimes very hard to debug, but now you'll know how to write "professional" macros :).

Andrei Ciobanu
A: 

Because a(i+1) gets preprocessed into (i+1*i+1).

And 3+3+1 = 7.

You might want to use parenthesis around x.

edit: Wow, is this redundant or what. :/

link|flag

hardware