C preprocessor

From OSDev.wiki
Revision as of 14:49, 19 July 2008 by osdev>Adek336
Jump to navigation Jump to search

C preprocessor

The C preprocessor is a powerful tool and properly used may be very useful. The following have been checked to work in GCC.

Rules

Uses

Uses for debugging

Deleted Code

A code block may be commented out to delete it from the program, however nesting deleted fragments may reduce legibility with C++ style comments, and C comments do not nest at all. A better solution is to wrap the code in and #if 0-#endif block, where the conditional 0 means false :

#if 0
    print("memory state: ");
    print(mem->state);
    print("\nallocated blocks: ");
    print(mem->allocs);
#endif

Many editors, like VIm have by default syntax highlighting rules that treat such #if 0-#endif blocks as comments. The #if-#endif directives must be balanced, single-quotes characters must balance etc. so for deleting non-code text use comments instead.


Hazards of the C preprocessor

There is a number of counter-intuitive consequences of macros and macro expanding design. Macro Pitfalls

See also

External Links

The GNU C preprocessor manual: