I have two unrelated questions:
Is it possible to use #define
to 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 ( #define
macros 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:
#define INFINITE_PRINTF while (1) \
{ \
printf("looping..."); \
}
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 main
and elsewhere. Occasionally I'll use #define
for constants near where they'll be used, for clarity.
You can not only #define strings, people #define code. Although the creator of C++ frowns on use of the preprocessor.
I think main() is too high up for OS specific code. I would try and make functions/classes that wrap any OS specific code. The lower you can place OS specific code, the better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.