J.W said:
I am trying to compare to a defined constants in C, and I have simplified my program to the following..
#include "stdio.h"
#include "stdlib.h"
#define INVALID_VALUE -999;
int main(int argc, const char* argv[])
{
int test=0;
if(test==INVALID_VALUE) //The error line..
return INVALID_VALUE;
return 0;
};
And when I use gcc to compile. it gives out error "error: expected ‘)’ before ‘;’ token" Any reason that this cannot be done.
You've already gotten several good answers, but you may be wondering
*why* (most) declarations and statements require semicolons, but
#define doesn't.
The preprocessor can be thought of as a separate language from C.
It isn't really; it's just as much a part of C as anything else.
But the rules it follows are quite different from those of most of
the rest of the language.
The preprocessor is strongly line-oriented. A macro definition
(#define ...) must appear on a single logical line (which can span
multiple physical lines, but only if you use \ line-splicing).
This means that, unlike a declaration or statement, a #define
doesn't need a semicolon to terminate it; it's terminated by the
end of the line. And since a semicolon isn't a required part of
the syntax of a #define, it's treated as just another token; in
this case, it's part of the definition of INVALID_VALUE.
Another thing to watch out for is that a macro that's to be used
as an expression needs to be written carefully to avoid operator
precedence problems. Macros expand to token sequences, not to
expressions. Judicious use of parentheses can avoid most problems.
Usually this means that the entire definition should be parenthesized
(unless it's single token), and that each reference to a macro
parameter should be parenthesized. For example:
#define DOUBLE(x) ((x) * 2)
So in your program (assuming you want the value -999), you should
write:
#define INVALID_VALUE (-999)
(If the value were 999, the parentheses wouldn't be necessary.)