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
No edit summary
Line 2: Line 2:
The C preprocessor is a powerful tool and properly used may be very useful.
The C preprocessor is a powerful tool and properly used may be very useful.
The following have been checked to work in GCC.
The following have been checked to work in GCC.
=== Uses for debugging ===
=== Hazards of the C preprocessor ===


=== 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 :
[code]
#if 0
print("memory state: ");
print(mem->state);
print("\nallocated blocks: ");
print(mem->allocs);
#endif
[/code]
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.
[http://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls Macro Pitfalls]
== See also ==
== See also ==
* [[C]]
* [[C]]

Revision as of 14:46, 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 : [code]

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

[/code] 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: