Ray
2009-11-29 09:11:01 UTC
Hello,
In the following code, the statement x = --1; causes an lvalue error as
expected, but the statement x = -N; does not. I was actually surprised that
x = -N; did not cause an error since I had always assumed that macro
substitutions occurred in what amounted to "copy and paste" operations rather
than "pre-evaluations". In this case, however, the preprocessor seems to be
first evaluating -1, then decrementing the resultant value and assigning it
to x. Thus, the compiler never sees -N as --1. But of course if the
compiler is instructed to produce a preprocessed (.i) file, x = -N; will be
converted to x = --1; in that file.
So, my question is this: Is the preprocessor required to produce a
pre-computed value for each macro whenever possible before that macro is
actually used, or is it within its rights to simply do a copy and paste until
all substitutions have been made, thus producing --1 in this case, which
would then cause a compiler error? Of course, if the macro were written
correctly (with parentheses) it wouldn't matter.
Thanks,
Ray
#define N -1
void fcn2()
{
int x;
x = -N;
x = --1;
}
In the following code, the statement x = --1; causes an lvalue error as
expected, but the statement x = -N; does not. I was actually surprised that
x = -N; did not cause an error since I had always assumed that macro
substitutions occurred in what amounted to "copy and paste" operations rather
than "pre-evaluations". In this case, however, the preprocessor seems to be
first evaluating -1, then decrementing the resultant value and assigning it
to x. Thus, the compiler never sees -N as --1. But of course if the
compiler is instructed to produce a preprocessed (.i) file, x = -N; will be
converted to x = --1; in that file.
So, my question is this: Is the preprocessor required to produce a
pre-computed value for each macro whenever possible before that macro is
actually used, or is it within its rights to simply do a copy and paste until
all substitutions have been made, thus producing --1 in this case, which
would then cause a compiler error? Of course, if the macro were written
correctly (with parentheses) it wouldn't matter.
Thanks,
Ray
#define N -1
void fcn2()
{
int x;
x = -N;
x = --1;
}