Macro Definition Usage Analysis

Source: Internet
Author: User
? macro definition:

In a program, the start of a # is a preprocessing instruction.
#define定义宏常量can appear anywhere in the code.

As shown in the following illustration:

To determine whether the macro definition is correct, to take the content of the macro definition to the real program, according to the actual program.

#define从本行开始, this macro constant is available for subsequent code. The following is the order in which the text is ordered, not the sequence in which the program code runs.

use #undef to end a macro definition. This limits the scope of the macro definition.
For example:

int F1 (int a, int b)
{
    #define _MIN_ (A,b) ((a) < (b)? a:b) return
    _min_ (A, b);
    #undef _min_
}

You can define a macro definition in a function. To define a macro expression:

? #define表达式给有函数调用的假象, but not a function.
? #define表达式可以比函数更强大
? #define表达式比函数更容易出错

Illustrate the advantages of macro expressions:

#define DIM (Array) (sizeof (array)/sizeof (*array))
...
int a[] = {1, 2, 3, 4};
printf ("%d\n", DIM (a));

This program outputs an array of 4 elements, but if the function is used to represent the output, the result is 1. Because the function has a weakness, his arguments pass in just a pointer, sizeof (array) calculates the length of the address, the address is 32 bits 4 bytes, the result is 1, but the sizeof (array) in the macro expression evaluates to the number of bytes in the entire array.

For example, the side effects of a macro expression:

#define SUM (A, B) (a) + (b)
...
printf ("%d\n", SUM (1,2) * SUM (1,2));

The program is expected to output 9, but no, the program outputs 5 because the macro definition is text substitution and the result is: 1+2*1+2=5. Therefore, macro-definition expressions also have a lot of side effects.

macro Expressions vs. functions:
The macro expression is processed during the precompiled period and the compiler does not know the existence of the macro expression.
A macro expression completely replaces a formal parameter with an argument and does not perform any operations.
The macro expression does not have any "call" overhead. Save time space.
recursive definitions cannot appear in macro expressions.

This recursive writing is wrong, will be define in the second FAC as a function, will be an error.

examples show the benefits of macro definitions:

#include <stdio.h>
#include <malloc.h>

#define MALLOC (Type, x) (type*) malloc (sizeof (type) *x)
#define FOREVER () while (1)

#define The BEGIN {
#define END   }
#define FOREACH (i, M) for (i=0; i<m; i++

int main ()
{
    int array[] = {1, 2, 3, 4, 5};
    int x = 0;
    Int*p = MALLOC (int, 5);

    FOREACH (x, 5)
    BEGIN
        p[x] = array[x];
    End

    FOREACH (x, 5)
    BEGIN
        printf ("%d\n", P[x]);
    End

    FOREVER ();

    Free (p);

    printf ("Last printf...\n");

    return 0;
}

The above program does not look like the C language anymore. built-in macro definition


Some compilers do not have _ stdc _

Use built-in macros to define log macros , that is, to print related actions.

#include <stdio.h>
#define LOG (s) printf ("%s,%d,%s,%s\n", __file__,__line__,__date__,s)

void f ()
{
    LOG ("Enter f ()");
    LOG ("Exit f ()");
}

int main ()
{
    LOG ("Enter Main ()");
    f ();
    LOG ("Exit Main ()");
    return 0;
}
a detailed comparison of functions and macros:

Macros are expanded directly by preprocessing, and the compiler does not know that macros exist
A function is an entity that is compiled directly by the compiler, and the invocation behavior is determined by the compiler

Using macros multiple times can result in increased program code volume
? function is jump execution, so the amount of code does not increase
?
Macros are more efficient than functions because they are expanded directly, with no call overhead
The function call creates an activity record that is less efficient than the macro

? Macros are slightly more efficient than functions, but their side effects are huge and error-prone.
The function has no side effects, but the function needs to establish an active object and the efficiency is affected by the pass of the argument to the formal parameter.

The parameter of a macro can be a type name
The parameter of the function must be a fixed type, less efficient and error-prone
? macros can implement functions that functions cannot implement, such as counting the elements of an array

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.