C Language Macro Definition Summary

Source: Internet
Author: User
C language Macro definition summaryTurn from: http://topcool99.ycool.com/post.1797687.html

A macro defines an identifier that represents a specific content.
The preprocessing process replaces the value of the macro identifier that appears in the source code with the macro definition.
The most common use of macros is to define global symbols that represent a value.
The second use of a macro is to define a macro with parameters, such that a macro can be called like a function, but it expands the macro at the call statement and replaces the formal arguments in the definition with the actual arguments at the time of the call.

1. #define指令
#define MAX_NUM 10
int Array[max_num];
for (i=0;i<max_num;i++)/*......*/
#define VERSION "version 1.0 Copyright (c) 2003"
2. #define INSTRUCTION with parameters
#define Is_even (N) ((n)%2==0)
#define MAX (X,y) (x) > (y)? (x):(y))
#define CUBE (x) (x) * (x) * (x)
can be any numeric expression or even a function call to replace the parameter x.
3. #运算符
#的功能是将其后面的宏参数进行字符串化操作 (stringfication), simply put a double quotation mark around it by replacing the macro variable it refers to. For example:
#define _STR (s) #s
#define WARN_IF (EXP) \
do{if (EXP) \
fprintf (stderr, "Warning:" #EXP "\ n");} \
while (0)

In practice, the replacement procedure shown below will appear:
warn_if (divider = 0);

is replaced by

Todo
{
if (divider = 0)
fprintf (stderr, "Warning" "divider = = 0" "\ n");
while (0);
This will output a message on the standard error stream each time the divider (divisor) is 0.

Again, for example, the following example:
#define FILL (a) {A, #a}

Enum Idd{open, close};
typedef struct msg{
IDD ID;
const char * MSG;
}msg;

MSG _msg[] = {Fill (OPEN), fill (close)};
Equivalent:
MSG _msg[] = {{Open, ' open '},
{Close, ' close '}};

4.# #运算符
# #运算符用于把参数连接到一起.
The preprocessor appears in # #两侧的参数合并成一个符号.
Look at the following example:

#define NUM (A,b,c) a# #b # #c
#define STR (A,b,c) a# #b # #c

Main ()
{
printf ("%d\n", NUM (1,2,3));
printf ("%s\n", STR ("AA", "BB", "CC"));
}

The final program output is:
123
Aabbcc
Look at the following example:
struct command
{
char * name;
void (*function) (void);
};

#define COMMAND (name) {name, name # # _command}

You then use some predefined commands to easily initialize an array of command structures:

struct command commands[] = {
COMMAND (quit),
COMMAND (Help),
...
}

The command macro acts as a code generator here, which reduces code density to some extent, and indirectly reduces the error caused by the failure to pay attention. We can also n a # #符号连接 n+1 a token. Like what:
#define Link_multiple (a,b,c,d) a# #_ # #b # #_ # #c # #_ # #d

typedef struct _RECORD_TYPE link_multiple (name,company,position,salary);
Here the statement expands to:
typedef struct _RECORD_TYPE name_company_position_salary;
5. Special Macro Compiler command
#error指令将使编译器显示一条错误信息, and then stop compiling.
#line指令可以改变编译器用来指出警告和错误信息的文件号和行号.
#pragma指令没有正式的定义. The compiler can customize its purpose. A typical use is to prohibit or allow some annoying warning messages.
... In the C macro, called Variadic Macro, the variable parameter macro.

Compiled on DEC 1996 at 22:18:48

 6. Predefined macros
__line__ the number of rows of the compiled file
__FILE__ the name of the file being compiled
__date__ compiled date (format "Mmm dd yyyy")
__time__ compile time (format "HH:MM:SS")
__stdc__ if the compiler accepts standard C, then the value is 1

printf ("Compiled on%s at%s\n", __date__, __time__);

Each time the program starts executing, the program is displayed to confirm the version:
Compiled on DEC 1996 at 22:18:48
The following macros can help us pinpoint the root cause of the error:

#define Check_zero (divisor) \
if (divisor = 0) \
printf ("* * attempt to divide by zero in line%d" \
"of file%s ***\n", __line__, __file__)

Check_zero macros should be invoked before the division operation:
Check_zero (j); k = i/j;
If J is 0, it will display the following form of information:
Attempt to divide by zero on line 9 of file FOO.C * * * *
A generic, macro--assert macro for error detection;

  7. Note
    macro names and parameters can not have spaces between the brackets,
  macro substitution for substitution only, do not calculate, do not do expression solution;
  Function call occurs when the compiled program runs. and allocates memory. The macro substitution is done before compiling, the memory is not allocated, the
  function has only one return value, and the macro can manage to get multiple values;
  Macro expansion makes the source program longer, and the function call does not;
  Macro expansion does not take up running time, for compile time only, function call takes up running time (allocate memory, preserve field, value Pass, return value);
  use conditional compilation to make the target program smaller and run shorter;
  precompilation increases the number of problems or algorithmic solutions, Help us select the right solution.

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.