C preprocessor: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
mNo edit summary
Line 9: Line 9:
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 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 :
A better solution is to wrap the code in and #if 0-#endif block, where the conditional 0 means false :
#if 0
[code]
#if 0
print("memory state: ");
print("memory state: ");
print(mem->state);
print(mem->state);
print("\nallocated blocks: ");
print("\nallocated blocks: ");
print(mem->allocs);
print(mem->allocs);
#endif
#endif
[/code]
Many editors, like [[VIm]] have by default syntax highlighting rules that treat such #if 0-#endif blocks as comments.
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.
The #if-#endif directives must be balanced, single-quotes characters must balance etc. so for deleting non-code text use comments instead.

Revision as of 14:49, 19 July 2008

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: