
  
C and C++
Main page
Defines
Design
Loops
Pointers
Punctuation
Strings
Stuctures
Types
SITE SERVICES
Contact Us
Search
Site Map
|
Home —» C and C++> —» #define
C Tutorial: #Defines
Using #define:
The #define statement is a precompiler directive: it changes the source code before it's compiled. In a #define directive, the string following the word #define, up to the first blank, is replaced by the model statement that follows, if any. In practice, #defines are used for simple text substitution, as macros, or as input to conditional compiler directives.
Standard shop #defines are usually stored in an #include member of a maclib library. You can also place #defines in a program, usually near the top. Each #define will stay in effect until a matching #undef statement or the end of the progam. It's considered good style to use all UPPERCASE for a #define.
Simple Substitution:
The simplest #defines equate two strings to make the code more readable or maintainable.
#define TRUE 1
#define FALSE 0
#define TABLESIZE 100
int partslist[TABLESIZE], i, editerror = FALSE;
if (editerror == TRUE)
{
RejectPart(partslist[i]);
}
else;
Many shops #define boolean conditions. It makes the code more readable and reduces the chances that you'll type = or &, instead of == or &&.
#define EQ ==
#define GE >=
#define AND &&
#define OR ||
if (editerror EQ TRUE OR i GE TABLESIZE)
#Defining Macros:
C lets you #define a pattern to generate in-line code. There are a number of gotchas in #defining macros (involving parentheses, blanks and semi-colons), so we'll give a simple example and recommend you to any of the standard C books. This example #defines a macro called blank, that takes one parameter.
#define blank(c) memset(c, ' ', sizeof(c))
This lets you code:
blank(name);
and have the compiler read it as:
memset(name, ' ', sizeof(name));
Using #defined Strings in Conditional Compiles:
Commenting out code in C is complicated by the fact that most compilers won't allow nested comments. The simpler way to comment out large sections of code is to #define a literal and use it with condional statements (#ifdef, #ifndef, #else, #elif, and #endif) to bypass specified lines entirely. The following code will cause the compiler to ignore all lines from #ifndef through #endif, as long as JSMITH is #defined.
#define JSMITH
#ifndef JSMITH
/* empty the catalog */
clearpartslist();
/* reload catalog from updated master file */
loadpartslist();
#endif
This technique is also useful for eliminating debugging code in the product version. The #define statement can be saved in an #include member, so you don't have to go into the code itself, just change the #define member and recompile.
#Define or Typedef?
Typedef is used to define new variable types. There are cases where using #define would give wrong results. The code below declares two variables of type pointer to char. Replacing the typedef with #define STRING char * would cause the second variable to be of type char, rather than pointer to char.
typedef char * STRING
STRING aptr, bptr;
|