Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
260
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 6: C Functions and Program Structures

Preprocessor

The preprocessor runs before the compiler as a separate first step.

#include

We have already discussed a little about #include but now would be a good time to mention how to use them to prevent name conflicts. We saw in the discussion on ‘extern’ that it is possible to forget that you have declared a variable name as an extern in one file then define a variable using a name in another file causing potential fun results, that is if you consider it fun to stay up all night to find a stupid bug. One way to lessen the likelihood of such is to keep a single header file for all the C source files in a program. For instance, if we decide to build a Killer Cylon Robot, we might want to write different C sources files for the various components we need:

CylonEyes.c

CylonLegs.c

CylonArms.c

CylonBlaster.c

CylonEnemyDiscriminator.c and so forth…

We could create a header file CylonKillerRobot.h, include it in each of the project files and use it for all of our variable and function declarations. By putting all the definitions in this file we lessen the likelihood of creating a name conflict that causes the code in CylonEnemyDiscriminator.c to substititute ‘theProgrammer’ for ‘theEnemy’ leading to the programmer learning that bugs yield extreme boinking.

#define

The #define directive tells the preprocessor to substitute a specified arbitrary sequence of characters anywhere it sees a specific token:

#define token arb1 arb2 arb3

Which causes the complier to substitute ‘arb1 arb2 arb2’ everywhere it sees ‘token’.

94