Using a macro leads to incorrect output when used as part of a larger mathematical expression - why is this happening?

This is the usual normal program that I found out in some sort of questions bank. The following is shown:

#define CUBE(p) p*p*p main() { int k; k = 27 / CUBE(3); printf("%d", k); } 

According to my understanding and knowledge, the value of K should be equal to 1, since CUBE (3) will be replaced by 3 * 3 * 3 during preprocessing, and after subsequent compilation it will give a value of 1, but instead it has a value of 81, which made me curious to know how this happened.

Maybe someone can justify answer 81 to this question above.

+6
c macros
source share
13 answers

Preprocessors must be adjusted in parentheses. Replace it

 #define CUBE(p) ((p)*(p)*(p)) 

and see.

+3
source share

The preprocessor just replaces

 CUBE(3) 

from

 3*3*3 

So you are done:

 k=27/3*3*3 

Which, evaluated left to right with operator priority, is actually 81.

If you add parenthesees around the macro, you should find the correct results:

 #define CUBE(p) (p*p*p) 

It would be even better to surround each instance p parental semantics as well:

 #define CUBE(p) ((p)*(p)*(p)) 

This will allow you to correctly pass expressions to the macro (e.g. 1 + 2 ).

+15
source share

Due to operator priority 27/3 27/3*3*3 = 81

You can use instead

 inline int cube(int p) { return p*p*p; } 
+4
source share

C macros perform textual substitution (i.e., equivalent to copying and pasting code). So your code comes from:

 k=27/CUBE(3); 

to

 k=27/3*3*3; 

Separation and multiplication have the same priority and have associativity from left to right, so this is analyzed as:

 k=((27/3)*3)*3; 

which is 9 * 3 * 3 = 81.

This is why C macros should always be defined using liberal parentheses:

 #define CUBE(p) ((p) * (p) * (p)) 

For more information, see http://c-faq.com/cpp/safemacros.html from the comp.lang.c. reference

+2
source share

Since macros are textual substitution that works:

 k = 27 / 3 * 3 * 3; 

Since multiplication and division occur from left to right, this works:

 k = ((27 / 3) * 3) * 3; 

So you want to change this in two ways:

 #define CUBE(p) ((p)*(p)*(p)) 

External brackets force multiplications before any other operations.

The brackets around a single p are for the case when you do:

 CUBE(1 + 2); 

Without these internal brackets, operator priority will put you in order.

+2
source share

Your macro is not protected. Try

 #define CUBE(p) ((p)*(p)*(p)) 

The current macro has been expanded to

 k=27/3*3*3 

which is equal to ((27/3) * 3) * 3

+1
source share
  #define CUBE(p) p*p*p main() { int k; k=27/CUBE(3); printf("%d",k); } 

According to my understanding and knowledge, the value of K should be equal to 1, since CUBE (3) will be replaced by 3 * 3 * 3 during pre-processing

YES

and after subsequent compilation it will give a value of 1, but instead it showed a value of 81, which made me curious to find out how it happened.

NOT,

 k= 27/3*3*3 =(((27/3)*3)*3) (The precedence of `*` and `/` are same but the associativity is from left to right) =((9*3)*3) =81 

Replace #define CUBE(p) p*p*p with #define CUBE(p) ((p)*(p)*(p))

0
source share
 k=27/CUBE(3); => k=27/3 * 3 * 3; 

Do you see it? CUBE should be defined as follows:

 #define CUBE(p) ((p)*(p)*(p)) 
0
source share

When you execute macros, you have to be careful how you place parentheses. In this case, you do not have them, so the expression becomes 27/3 * 3 * 3, which according to the rules of priority / and * becomes (27/3) * 3 * 3.

0
source share

27/3 * 3 * 3 = 9 * 3 * 3 = 81?

0
source share

Both operators and / have the same priority. To do 3 * 3 * 3 first, you shoorl enclose them in parentheses.

 #include <stdio.h> #define CUBE(p) p*p*p int main () { int k; k=27/(CUBE(3)); printf("%d",k); return 0; } 
0
source share

his way of implementing associativity and operator precedence. when the expression expands, it becomes 27/3 * 3 * 3, and not 27 / (3 * 3 * 3) now division and multiplication have the same priority in C, but associativity is left to the right for both. therefore, it can be shown as: (27/3) * 3 * 3, which, in turn, is equal to (9 * 3) * 3 = 81 also, if you remember the old BODMAS arithmetic rule (subtraction of the offset with subtraction of the brackets) , this order of priority, then we do the division first, and then the multiplication. so again we get the answer as 81 for 27/3 * 3 * 3.

0
source share

Hi answer for this: 81 Explanation: At stage k = 27 / cube (3), cube (3) is replaced with a 3 * 3 * 3 preprocessor. Then the operator above becomes k = 27/3 * 3 * 3 in that the expression 27/3 is evaluated with the compiler (operator priority), the result is (27/3): 9 statement k = 27/3 * 3 * 3 becomes equal k = 9 * 3 * 3; result for expression above 81:

0
source share

All Articles