1
#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. (I'm getting the output 7.)


I understood the point very much but if the program is 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 does the above program give the following output:

9
49
2
  • 1
    @Paul What does it matter in this particular case if it's the context of homework? Maybe it is, maybe the OP has very well reduced the problem to a minimum program that exhibits the behavior that puzzles them. I'm not saying it's a good question, I'm saying that (for once) it's not a bad question because it may or may not be homework. Unless the homework is "why does this program print 7?", in which case it's a bad question for being homework. Commented Apr 25, 2010 at 8:21
  • 1
    @Pascal: the main reason is that it's homework then it's better to give helpful hints or general guidance rather than complete solutions, so that you help the student to learn something. If it's not homework then obviously a complete solution is appropriate. Commented Apr 25, 2010 at 9:02

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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 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.
2

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

Comments

2

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

1 Comment

can u tell me the problem with the new macros
1

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. :/

Comments

0

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

Comments

0

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 :).

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.