hi,
what is the difference between the 2 constructions
#define (value), and,
#define value
thx in advance!
hi,
what is the difference between the 2 constructions
#define (value), and,
#define value
thx in advance!
The first one can be used to declare constants
Whereas the second one can be used to declare functionsCode:#define PI 3.14159
orCode:#define RADTODEG(x) ((x) * 57.29578)
Note in this one, every element must be nested inside parentheses in order to avoid faulty comparison.Code:#define MIN(a,b) ((a)>(b)?(b):(a))
eg: if it were defined like this
then the following code -Code:#define MIN(a,b) (a>b?b:a)
which will not work as expected due to operator precedence and would expand toCode:int i = MIN(1,1+1);
Code:1 > 1 + 1 ? 1 + 1 : 1;
It would be better worded: The first one declares a macro which you do not pass arguments to, while the second allows you to pass arguments.
Neither however, actually declares a function. It may seem like it's function-like, but it's not the same as a function. For one, there is no type checking...
Quzah.
Hope is the first step on the road to disappointment.
Ah, Yes. My Mistake.It would be better worded: The first one declares a macro which you do not pass arguments to, while the second allows you to pass arguments.
Neither however, actually declares a function. It may seem like it's function-like, but it's not the same as a function. For one, there is no type checking...
thx guys. but if i get things like
does that mean you can actually pass arguments to 'OK' and 'NOT_OK'? i was under the impression that these are constants.Code:#define OK (0) #define NOT_OK (-1)
No. The space between the macro name and the opening parenthesis means that the macro is not a function style macro.Originally Posted by bored_guy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hey thx ppl that pretty much clears it
It is good practice to put parentheses around your definitions if they're anything more than a simple number. Then you can be sure that the definition is evaluated as a single unit wherever the preprocessor drops it in.
For example:
#define A 1
#define B 2
#define C (A + B)
You'd want to make sure to preserve the desired order of operations wherever C is expanded in the code.