-
#
directive, has no effect -
#include
includes a source code file -
#define
defines a macro -
#undef
undefines a macro -
#if
if the given condition is true, compile the following code -
#ifdef
if the macro is defined, compile the following code -
#ifndef
if the macro is not defined, compile the following code -
#elif
if the previousif
condition is not true, and the current condition is true, compile the following code -
#endif
ends anif...#else
compilation block -
#error
stops compilation and displays an error message
What are Preprocessor Directives?
-
The first method is to enclose the header file in angle brackets.
#include <stdio.h>
-
The second method is to enclose the header file in double quotes.
#include "test.h"
#define
macro is widely used in C language, but due to some shortcomings of #define
, C++ emphasizes using const
to define constants.#define
:-
Using #define to implement macros for finding maximum and minimum values
#include <stdio.h>
#define MAX(x,y) (((x)>(y))?(x):(y))
#define MIN(x,y) (((x)<(y))?(x):(y))
int main(void)
{
#ifdef MAX // Check if this macro is defined
printf("3 and 5 the max is:%d\n",MAX(3,5));
#endif
#ifdef MIN
printf("3 and 5 the min is:%d\n",MIN(3,5));
#endif
return 0;
}
/*
* (1) The ternary operator is more efficient than if-else.
* (2) Be careful when using macros; parameters should be carefully enclosed in parentheses,
* because macros are just simple text replacements and can lead to ambiguous errors if not handled properly.
*/
-
Incorrect usage of macro definitions
#include <stdio.h>
#define SQR(x) (x*x)
int main(void)
{
int b=3;
#ifdef SQR// Only the macro name is needed, no parameters required; parameters will cause warnings
printf("a = %d\n",SQR(b+2));
#endif
return 0;
}
/*
* First of all, this macro definition is incorrect. It does not calculate the square of B+2 in the program.
* During preprocessing, it is replaced with the following result: b+2*b+2
* The correct macro definition should be: #define SQR(x) ((x)*(x))
* Therefore, it is advisable to enclose parameters in parentheses to avoid ambiguity.
*/
-
Connecting macro parameters
#include <stdio.h>
#define STR(s) #s
#define CONS(a,b) (int)(a##e##b)
int main(void)
{
#ifdef STR
printf(STR(VCK));
#endif
#ifdef CONS
printf("\n%d\n",CONS(2,3));
#endif
return 0;
}
/* (Most of these are rarely used; if needed, refer to the manual)
* The first macro converts the parameter into a string using #.
* The second macro concatenates two macro parameters together using ##, resulting in aeb, 2e3 which means 2000.
*/
-
Using macros to obtain the high or low byte of a word
#include <stdio.h>
#define WORD_LO(xxx) ((byte)((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte)((word)(xxx) >> 8))
int main(void)
{
return 0;
}
/*
* A word consists of 2 bytes; to get the low byte (the low 8 bits), perform a bitwise AND with 255 (0000,0000,1111,1111).
* To get the high byte (the high 8 bits), simply right shift by 8 bits.
*/
-
Using macros to get the number of elements in an array
#include <stdio.h>
#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
int main(void)
{
int array[100];
#ifdef ARR_SIZE
printf("array has %d items.\n",ARR_SIZE(array));
#endif
return 0;
}
/*
* Total size divided by the size of each type.
*/
-
Example 1:
#include <stdio.h>
#include <stdlib.h>
#define DEBUG
int main(void)
{
int i = 0;
char c;
while(1)
{
i++;
c = getchar();
if('\n' != c)
{
getchar();
}
if('q' == c || 'Q' == c)
{
#ifdef DEBUG// Check if DEBUG is defined
printf("We get:%c, about to exit.\n",c);
#endif
break;
}
else
{
printf("i = %d",i);
#ifdef DEBUG
printf(", we get:%c",c);
#endif
printf("\n");
}
}
printf("Hello World!\n");
return 0;
}
/*#endif is used to terminate the #if preprocessing directive.*/
-
Usage of ifdef and #ifndef
#include <stdio.h>
#define DEBUG
main()
{
#ifdef DEBUG
printf("yes ");
#endif
#ifndef DEBUG
printf("no ");
#endif
}
//#ifdef defined is equivalent to #ifdef;
//#if !defined is equivalent to #ifndef
-
#else directive

-
#elif directive

-
Other directives
-
#error
directive will cause the compiler to display an error message and then stop compilation. -
#line
directive can change the file number and line number used by the compiler to indicate warning and error messages. -
#pragma
directive has no formal definition. Compilers can customize its use. A typical usage is to suppress or allow certain annoying warning messages.
👆Long press the image to scan the code to apply👆