Does anyone know of a tutorial or a lesson on Preprocessor Directives?
I looked at MSDN Visual C++ refrence material but it is written in such a way that I barely understand it. I am looking for something that explains it in plain english!
Printable View
Does anyone know of a tutorial or a lesson on Preprocessor Directives?
I looked at MSDN Visual C++ refrence material but it is written in such a way that I barely understand it. I am looking for something that explains it in plain english!
Here is one: http://www.cplusplus.com/doc/tutorial/preprocessor.html
There really aren't many preprocessor directives. The ones you will most likely use are:
Honestly, I never really used anything else but those.Code:#define
#include
#ifdef
#else
#endif
#if
#pragma
How much explaining does #include, #define and #if take?
#define is quite complex - macro substitution has some strange rules.
There's also #error, which is very simple.
I've never used #line myself, but I've used tools which have (i.e. bison).
I've often used defined(), which isn't a preprocessor directive, but it is related. And what about #elif? :)
(Side question: are you supposed to put double-quotes around the text after a #error directive?)
(Side answer: no.)
defined() is a preprocessor keyword.
#if cond1
#elif cond2
#endif
==
#if cond1
#else
#if cond2
#endif
#endif
Some other preprocessors don't support #elif, by the way.
(Aside: thank you.)
As far as I know, #elif is ANSI standard . . . are you referring to other types of preprocessors, or what?
Old, old, old preprocessors. But they still exist in some unices.
I believe there is also #ifndef, #undef and the string-ify operation.
QuantumPete
Stringify, yes, and token concatenation.
Don't forget the Preprocessor variables too, like __LINE__ and such, if you want to learn about the Preprocessor capabilities. I used __LINE__ just the other day writing a return code checker macro when writing a C program that uses a lot of the new "xxxx_s" safe functions.