#define, #include in C
C provides a very limited macro facility. You can say
The other control word known to C is #include. To include one file in your source at compilation time, say
#define name somethingand thereafter anywhere ``name'' appears as a token, ``something'' will be substituted. This is particularly useful in parametering the sizes of arrays:
#define ARRAYSIZE 100 int arr[ARRAYSIZE]; ... while( i++ < ARRAYSIZE )...(now we can alter the entire program by changing only the define) or in setting up mysterious constants:
#define SET 01 #define INTERRUPT 02 /* interrupt bit */ #define ENABLED 04 ... if( x & (SET | INTERRUPT | ENABLED) ) ...Now we have meaningful words instead of mysterious constants. (The mysterious operators `&' (AND) and `|' (OR) will be covered in the next section.) It's an excellent practice to write programs without any literal constants except in #define statements.There are several warnings about #define. First, there's no semicolon at the end of a #define; all the text from the name to the end of the line (except for comments) is taken to be the ``something''. When it's put into the text, blanks are placed around it. Good style typically makes the name in the #define upper case; this makes parameters more visible. Definitions affect things only after they occur, and only within the file in which they occur. Defines can't be nested. Last, if there is a #define in a file, then the first character of the file must be a `#', to signal the preprocessor that definitions exist.
The other control word known to C is #include. To include one file in your source at compilation time, say
#include "filename"This is useful for putting a lot of heavily used data definitions and #define statements at the beginning of a file to be compiled. As with #define, the first line of a file containing a #include has to begin with a `#'. And #include can't be nested -- an included file can't contain another #include.
0 comments:
Post a Comment