#define CUBE(x)(x*x*x)
int main()
{
int a, b;
b = 3;
a = CUBE(b++)/b++;
printf(a=%d b=%d\n", a, b);
return 0;
}
I have a confusion in this Macro defining statement? and I need output too?
#define CUBE(x)(x*x*x)
int main()
{
int a, b;
b = 3;
a = CUBE(b++)/b++;
printf(a=%d b=%d\n", a, b);
return 0;
}
I have a confusion in this Macro defining statement? and I need output too?
The part
CUBE(b++)
will be converted to
(b++ * b++ * b++)
which is undefined behavior or problematic due to modifying a variable in one single statement. It's recommended to avoid such a thing.
Try to pass a variable without ++
or --
.
a=CUBE(b++)/b++;
|
V
a=(b++ * b++ * b++)/b++ ;
In the above expression b value modifies between sequence points would cause undefined behavior due to lack of Sequence_point
To avoid this first assign and then increment
a=CUBE(b)/b;
b++;
Use #define CUBE(x) ((x)*(x)*(x))
to avoid mistake when x
is an expression.
a = CUBE(b++)/(b++);
The value of a after executing the statement depends on the compiler you use or something else. This is called undefined behavior
.