Macro extension in the context of an arithmetic expression?

I saw this code below on the website. I could not understand how the result comes as 11 , not 25 or 13 .

Why I think 25 , because SQ(5) 5*5

or 13 because

SQ(2) = 4;

SQ(3) = 9;

maybe the end result will be 13 (9 + 4) But I am surprised to see the result as 11 . How is the result like 11 ?

 using namespace std; #define SQ(a) (a*a) int main() { int ans = SQ(2 + 3); cout << ans << endl; system("pause"); } 
+6
source share
6 answers

#define expands before the compiler sees the source code. That's why they are called preprocessor directives, here the processor is a compiler that translates C into machine-readable code.

So this is what the macro preprocessor is passed to the compiler:

SQ(2 + 3) expands as (2 + 3*2 + 3)

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

How can you make him do what you expect?

  • Provide an assessment procedure. Use () either in the macro definition or in the macro call. OR
  • Write a simple function that does the job
+6
source

The preprocessor performs a simple replacement of the text in the source code. He knows nothing about the base language or its rules.

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

A more reliable way to determine SQ :

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

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

Although this definition is an improvement, it is still not bulletproof. If SQ() been applied to an expression with side effects , this can have undesirable effects. For instance:

  • If f() is a function that outputs something to the console and returns int , SQ(f()) , the result will be printed twice.
  • If i is an int variable, SQ(i++) leads to undefined behavior .

See Macros for more examples of macro difficulties.

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

+19
source

The C preprocessor performs textual substitution before the compiler interprets C expressions and syntax in general. Therefore, starting the C preprocessor on this code converts:

 SQ(2 + 3) 

in

 2 + 3*2 + 3 

which simplifies:

 2 + 6 + 3 

which is equal to 11.

+3
source

It is just a replacement before compilation.

so you should try:

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

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

But correcting this, as I wrote above, it will be like ((2+3)*(2+3)) , which 5*5 = 25 , what you want.

+1
source

#define preprocessor

Syntax: # define ID replacement

  • When the preprocessor encounters this directive, it replaces any occurrence of the identifier in the rest of the code with a replacement.
  • This replacement can be an expression, expression, block, or just something.
  • The preprocessor does not understand C, it simply replaces any occurrence of the identifier with a replacement.

# define can also work with parameters for defining function macros:

# define SQ(a) (a*a)

will replace any SQ (a) event with a * a at compile time. Consequently,

SQ (2 + 3) will be replaced by 2 + 3 * 2 + 3 Calculation is performed after completion of the replacement. hence the answer 2 + 3 * 2 + 3 = 11

+1
source

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

You must define it as:

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

Tested on gcc , for type inputs

  • constants
  • variable,
  • Permanent + Const
  • Const + variable
  • variable ++ / ++ variable
  • function call containing printf.

Note: typeof is a complement to the GNU C standard. It may not be available on some compilers.

+1
source

All Articles