Take the 2-minute tour ×

I saw this below code in an website. I could not able to understsnd how the result is coming as 11, instead of 25 or 13.

Why I am thinking 25 because SQ(5) 5*5

or 13 because

SQ(2) = 4;

SQ(3) = 9;

may be final result will be 13 (9 + 4) But surprised to see result as 11. How the result is coming as 11?

using namespace std;
#define SQ(a) (a*a)
int main()
    int ans = SQ(2 + 3);
    cout << ans << endl;
share|improve this question
It would be nice if the title could be changed to something more descriptive. Perhaps "Macro expansion in context of arithmetic expression" or something like that. –  jogojapan Dec 5 '12 at 7:38
@jogojapan: Yes You are right, i will change the Title as you have directed –  Rasmi Ranjan Nayak Dec 5 '12 at 8:30

6 Answers 6

up vote 6 down vote accepted

#define expansions kick in before the compiler sees the source code. That is why they are called pre-processor directives, the processor here is the compiler that translates C to machine readable code.

So, this is what the macro pre-processor is passing on to the compiler:

SQ(2 + 3) is expanded as (2 + 3*2 + 3)

So, this is really 2 + 6 + 3 = 11.

How can you make it do what you expect?

  1. Enforce the order of evaluation. Use (), either in the macro definition or in the macro call. OR
  2. Write a simple function that does the job
share|improve this answer

The preprocessor does a simple text substitution on the source code. It knows nothing about the underlying language or its rules.

In your example, SQ(2 + 3) expands to (2 + 3*2 + 3), which evaluates to 11.

A more robust way to define SQ is:

#define SQ(a) ((a)*(a))

Now, SQ(2 + 3) would expand to ((2 + 3)*(2 + 3)), giving 25.

Even though this definition is an improvement, it is still not bullet-proof. If SQ() were applied to an expression with side effects, this could have undesired consequences. For example:

  • If f() is a function that prints something to the console and returns an int, SQ(f()) would result in the output being printed twice.
  • If i is an int variable, SQ(i++) results in undefined behaviour.

For further examples of difficulties with macros, see Macro Pitfalls.

For these reasons it is generally preferable to use functions rather than macros.

share|improve this answer
A more robust way is to use a function for this. –  KillianDS Dec 5 '12 at 7:32
@KillianDS parens are safe –  Jan Dvorak Dec 5 '12 at 7:32
@JanDvorak: They are, until one accidentally applies SQ() to an expression with side effects. –  NPE Dec 5 '12 at 7:39
@fersarr: Consider SQ(f()). If f() prints something to the console, you'd unexpectedly see the output twice. –  NPE Dec 5 '12 at 7:46
Macros are never safe, and besides, they are a pain to debug. –  DevSolar Dec 5 '12 at 8:10

The C preprocessor does textual substitution before the compiler interprets expressions and C syntax in general. Consequently, running the C preprocessor on this code converts:

SQ(2 + 3)


2 + 3*2 + 3

which simplifies to:

2 + 6 + 3

which is 11.

share|improve this answer

#define preprocesor

Syntax : # define identifier replacement

  1. When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement.
  2. This replacement can be an expression, a statement, a block or simply anything.
  3. The preprocessor does not understand C, it simply replaces any occurrence of identifier by replacement.

# define can work also with parameters to define function macros:

# define SQ(a) (a*a)

will replace any occurance of SQ(a) with a*a at compile time. Hence,

SQ(2+3) will be replaces by 2+3*2+3 The computation is performed after the replacement is done. hence answer 2+3*2+3=11

share|improve this answer

For your implementation, the value will expand to 2+3 * 2+3 which will result into 2+6+3=11.

You should define it as:

#define SQ(x) ({typeof(x) y=x; y*y;})

Tested on gcc, for inputs like

  1. constants,
  2. variable,
  3. constant+const
  4. const+variable
  5. variable++ / ++variable
  6. function call, containing printf.

Note: typeof is GNU addition to standard C. May not be available in some compilers.

share|improve this answer
I guess you mean 2+6+ 3 =11. ;) –  Skalli Dec 5 '12 at 8:32
Sorry, typo.... –  anishsane Dec 5 '12 at 8:34

It's just a replacement before compilation

so you should try this out :


In your case , SQ(2 + 3) is equivalent to (2+3*2+3) which is 11.

But correcting it to as I wrote above, it will be like, ((2+3)*(2+3)) which is 5*5 = 25 that's the answer you want.

share|improve this answer
Because everything is not fine... What happens when I call this macro as SQ(a++)? The result is ((a++)*(a++)). Is that what I intended? Chances are that no, it's not. You cannot reliably reference a macro argument more than once inside the macro. It's a recipe for disaster. –  Nik Bougalis Dec 5 '12 at 7:50
@NikBougalis : nice catch ........edited according to question –  Omkant Dec 5 '12 at 8:40

Your Answer


