Is there a good reason for always enclosing a define in parentheses in C?

Yes. The preprocessor concatenation operator (##) will cause issues, for example: #define _add_penguin(a) penguin ## a #define add_penguin(a) _add_penguin(a) #define WIDTH (100) #define HEIGHT 200 add_penguin(HEIGHT) // expands to penguin200 add_penguin(WIDTH) // error, cannot concatenate penguin and (100) Same for stringization (#). Clearly this is a corner case and probably doesn’t matter considering how WIDTH … Read more

Why would someone use #define to define constants?

#define has many different applications, but your question seems to be about one specific application: defining named constants. In C++ there’s rarely a reason to use #define to define named constants. #define is normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short, … Read more

What is the difference between #define and const? [duplicate]

The #define directive is a preprocessor directive; the preprocessor replaces those macros by their body before the compiler even sees it. Think of it as an automatic search and replace of your source code. A const variable declaration declares an actual variable in the language, which you can use… well, like a real variable: take … Read more

Namespaces in C

Another alternative would be to declare a struct to hold all your functions, and then define your functions statically. Then you’d only have to worry about name conflicts for the global name struct. // foo.h #ifndef FOO_H #define FOO_H typedef struct { int (* const bar)(int, char *); void (* const baz)(void); } namespace_struct; extern … Read more

Does the C preprocessor strip comments or expand macros first? [duplicate]

Unfortunately, the original ANSI C Specification specifically excludes any Preprocessor features in section 4 (“This specification describes only the C language. It makes no provision for either the library or the preprocessor.”). The C99 specification handles this explicity, though. The comments are replaced with a single space in the “translation phase”, which happens prior to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)