C cheatsheet

Page 11

The C Cheat Sheet

Revision 1.0

#include

September 5, 2000

<somefile.h>

The second form uses quotation marks instead of angle brackets: #include

“somefile.h”

The difference in these two forms lies in the include file search path. This is a list of directories that the preprocessor searches in order to find the include file. This search path should be configured when you install the compiler so you shouldn’t generally have to worry about it. With the second form of the #include directive (using quotation marks), the preprocessor first looks in the same directory as the source file. If the include file is not found there, then the search path is considered. Practically, the angle brackets form (e.g., <stdio.h>) is used to include system files that are part of the C compiler system, while the quotation marks form (e.g., “mystuff.h”) is used to include files that you write yourself and are part of your project. 1.4.4 The #ifdef/#else/#endif Directives There are several directives that can be used to effect conditional compilation. Most often, conditional compilation is used either as a debugging tool or to compile different code for different hardware architectures. Here is an example that shows how to add debugging statements that can easily be turned on or off. #include <stdio.h> #define DEBUG 1 void main(void) { #ifdef DEBUG printf(“Debugging is enabled.\n”); #else printf(“Debugging is disabled.\n”); #endif }

The #ifdef directive tells the preprocessor to pass the subsequent lines to the compiler only if the item following #ifdef has been defined using a #define directive. In this example, the token DEBUG has been defined to be 1, so the #ifdef statement succeeds. What the C compiler sees, then, is the following: void main(void) { printf(“Debugging is enabled.\n”); }

Everything following the #else directive is not passed to the compiler (up to the #endif directive).

6

Copyright © 2000 Andrew Sterian


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.