I have two unrelated questions:
-
Is it possible to use
#defineto define something other than a number? (Such as an extended ASCII character). -
Is it considered good practice to use preprocessor directives within the
main()function? The only reason I would ever think to do this is to execute different code depending on which OS is being run.
Object-like macros (
#definemacros with no arguments) are simply replacements. So anything that might otherwise be in your code can be the replacement, for example a literal string:#define PROGRAM_NAME "MyProgram", or multi-line code blocks. Here’s a useless example of the latter:As for the second question, it is common practice to use preprocessor directives throughout C code to do just what you’ve mentioned: conditionally including/excluding code, in
mainand elsewhere. Occasionally I’ll use#definefor constants near where they’ll be used, for clarity.