Why should one bother with preprocessor directives?

Viewed 9883

This question may seem rather basic, but coming from an engineering (non computer-science) background, I was unsure about what the snippets of '#'s were in some C++ code.

A quick search led me to the concise, well-explained cplusplus tutorial page on preprocessor directives.

But why bother with the concept of preprocessor directives at all? Is it not possible to write equivalent code that can assign values to constants, define subroutines/function/macros and handle errors?

I guess I ultimately want to know when it is good practice to use such preprocessor directives, and when it is not.

14 Answers

Unlike everybody else, I don't have a big problem with preprocessor directives. The only thing is that using preprocessor defines works better in C than C++. Eg: Win32, OpenGL, zip libraries, jni and many other c libraries use preprocessor directives. Eg: Win32 has "OPAQUE" and "TRANSPARENT" which is passed to their function SetBkMode(HDC,int); Now imagine how easily it is to want to use any of those words. You can't, because C preprocessor doesn't care about namespace. Why isn't there a Cpp preprocessor.

But I know the right tool for job.

Related