Macros inside parameters without parentheses error-prone, when used as far as possible parentheses
Program 1:
#include <stdio.h>
#define sqare(X) x*x
int Main ()
{
int n = ten;
int m= Sqare (n);
printf ("m=%d\n", m);
return 0;
}
Results:
m=100
Please press any key to continue ...
Analysis: There seems to be no problem, see the following two examples
Program 2:
#include <stdio.h>
#define sqare(X) x*x
int Main ()
{
int m = Sqare (1 + 3); //1+3*1+3=7
printf ("m=%d\n", m);
return 0;
}
Results:
M=7
Please press any key to continue ...
Corrective Procedure 2:
#include <stdio.h>
#define sqare(x) (x)* (x )
int Main ()
{
intm =Sqare(1+3);//(1+3)*(1+3)= -
printf ("m=%d\n", m);
return 0;
}
Results:
M=16
Please press any key to continue ...
Program 3:
#include <stdio.h>
#define ADD(X) x+x //No parentheses error-prone
int Main ()
{
int m = 10* ADD (2); //10*2+2
printf ("m=%d\n", m);
return 0;
}
Results:
M=22
Please press any key to continue ...
Corrective Procedure 3:
#include <stdio.h>
#define ADD(X) (x+x)
int Main ()
{
int Span style= "font-family: ' The new song Body '; Font-size:13px;background:rgb (255,255,255);" > m = 10* add (2); //10* Span style= "font-family: ' New song body '; Color:rgb (0,128,0); Font-size:13px;background:rgb (255,255,255);" > ( 2+2 )
printf ("m=%d\n", m);
return 0;
}
Results:
M=40
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1728067
C Language: Macros inside the parameters are error-prone, when used as far as possible parentheses and examples