blargg said:
The compiler/preprocessor does. Here are some problems with macros
used for constants.
A macro does general text replacement:
#define foo 1
struct bar { int foo; }; // error
Yes, sharp knives must be handled with care. Your above example is not
really a macro weakness, it's careless or naive programming is all.
Standards discipline are important and a bit of experience wouldn't hurt
either.
Expression must be parenthesized:
#define foo 1+1
int i = foo * 2; // unexpected result
Another example of a sharp knife being used by a child.
Avoiding name clashes by using convention of all-uppercase is ugly:
#define FOO 1 // ugly name
Yes, that example is ugly. But I don't find the following ugly at all:
#define MEM_PAGE_SIZE 4096
*One can even add some little trinket in the name to "ensure" uniqueness if
dealing with external code.
Macros aren't scoped:
void f1()
{
#define FOO 1
}
void f2()
{
#define FOO 2 // error, redefinition
}
No one in their right mind would do that and expect scoping! Again, you're
just showing untrained use of the preprocessor rather than inherent flaw
with text substitution. Sure, if the concept is difficult, avoid it. I don't
find it difficult at all.
Syntax differs from normal object declaration/initialization:
#define FOO 1
int bar = 1;
A minor point, but yeah, if one was just learning C (or teaching it!),
surely using 'const' is the way to go. But your examples seem to attempt to
make it look like the issues with defining constants via text substitution
are severe when they're really not. Note also that the preprocessor #define
That said, I recently started using consts instead of #defines,
but it really is a miniscule thing relative to other things.
[...]
Not if you use them in your library's header files, where they can
cause problems your client's code.
See the above response that I've asterisked to solve that simply.
Tony