C language Macro definition #define Usage summary.

Source: Internet
Author: User
1. Simple macro definition
#define Identifier substitution list (alternate list can be number, string literal, punctuation, operator, identifier, keyword, Word constants quantity.) Note: The substitution list is nullable.
Typical error:
#define N = 100
int a[n]; /* This will become an int a[= 100], which will be processed as an identification mark.


#define N 100;
int a[n]; /* The definition of a semicolon will become an int a[100;] This is a very common error.

#define PIN (int*);
Pin a,b;
int* a,b; /* The intention is both A and B are int pointers, but in fact it becomes int* a,b;a is an int pointer, and b is an int variable. This should use a typedef instead of define so that A and B are all int pointers. */

Typical ways to use:
Using a macro definition we can define or even change the grammar habits of C language according to our own habits, for example:
#define BEGIN {
#define END}
int main () BEGIN
printf ("DEFINE----\ n");
End

Define a loop
#define Loop for (;;)

Redefining data types
#define IT INT


2. Macros with parameters
#define Identifier (X1,X2,X3...XN) replaces the list (note: x1,x2,x3.) is a macro parameter with no spaces between the identifier and its left bracket
How to use:
#define MAX (X,y) (x) > (y)? ( x):(y))
I=max (J+K,M-N);
To be replaced by:
I=max (J+k) > (m-n)? ( J+K):(m-n));
#define SI_EX (N) ((n)%2==0)
if (SI_EX (i)) i++;
To be replaced by:
if (SI_EX ((i)%2==0))
We can see from the example above that the identifier takes a parameter (x1,x2,x3 ...). will be replaced with the list (Y1,y2,y3 ...). ) corresponding substitution, but not in order.


3. Special Single-line definition of macros
#define A (x) t_# #x
#define B (x) #@x
#define C (x) #x
X=1
To be replaced by:
A (1)------> t_1 (t_# #x, # #符号是记号粘黏符号, paste the characters before and after. )
B (1)------> ' 1 ' (#@x, #@ symbols will be literal quantization of the parameters of the macro, and add ' "number)
C (1)------> "1" (#x, #符号会将宏的参数进行字符串字面量化 and Plus "")


Multi-line definition for 4.define
Define can replace multiple lines of code, adding a "\" for each line change
#define MAX (x,y) do {\
Statement 1; \
Statement 2; \
* * The writing of the annotation * *
while (0)/* (no trailing;) * *


5. In the large-scale development process, especially cross-platform and the system software, define most important function is the conditional compilation.
#ifdef WINDOWS
......
......
#endif
#ifdef LINUX
......
......
#endif
You can set up the compilation environment by #define at compile time
6. Cancel Macros
#undef identifier


7. Conditional compilation
#ifdef XXX ... (#else) ... #endif


8. Predefined macros
A number of useful macros are included in the C language that provide the current compilation information.
__line__ the number of rows of the compiled file (integer)
__FILE__ the name of the compiled file (character type)
__date__ Compile date (character type)
__time__ compile time (character type)
__stdc__ if the compiler accepts standard C, then the value is 1. (Integral type)


Usually when we compile, we can use these predefined macros for scheduling errors, such as:
In general, when the compiler is not prompted when there is zero, we can customize a workaround.
#define Checkzero (divisor) \
if (divisor==0) \
printf ("***attempt to DIVIDE by ZERO in line%d of file%s ***\n", __line__,__file__); \


Reference method
Checkzero (j);
k=i/j;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.