SoFunction
Updated on 2025-03-05

Causes and analysis of errors in multi-line macro definition in C language

Reasons for errors in multi-line macro definition in C language #define

1. The first error

#include<>
#define echange(a,b) {\/*A situation where multiple lines of commands are allowed in the macro definition, you must add "\"*/ to the rightmost right
 int t;\
 t=a;\
 a=b;\
 b=t;\
}
main()
{
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

When using #define for multi-line macro definition, comments should be placed before "\"

2. The second error

#include<>
#define echange(a,b) {/*A situation where two clothes commands are allowed in the macro definition, you must add "\"*/ to the far right.\
 int t;\
 t=a;\
 a=b;\
 b=t;\
}/*Add an extra "\" to the last line*/\
main()
{
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

When using #define for multi-line macro definition, add "\" to the last line. When we use #define for multi-line definition, the next line of the last "\" is also within the scope of the macro definition. Just remove the "\" of the last line.

The following is the correct form of the code

#include<>
#define echange(a,b) {/*A situation where two clothes commands are allowed in the macro definition, you must add "\"*/ to the far right.\
 int t;\
 t=a;\
 a=b;\
 b=t;\
}
main()
{
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

A few tips for using #define macro definition

1. Debug switch

Sometimes when writing programs, in order to facilitate searching for errors, serial printing statements will be added in many places to intuitively see where the program is wrong.

However, the output of the serial port takes time. When the project is basically completed, these prints need to be turned off, but commenting out one line by one is time-consuming and labor-intensive, which is unrealistic.

At this time, you can use a macro as a "switch". By directly operating this macro, you can implement the printed switch, for example:

//#define    DEBUG(...)
#define        DEBUG    printf

In addition, several predefined macros can be used for assistance:

__FILE__            // document__FUNCTION__        // Function__LINE__            // Line number
DEBUG("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

2. Conditional Compilation

When the program is to adapt to more different environments, it can also use conditional macros to select different code modules:

#if 1
    printf("Hello!\n");
#else
    printf("Hi!\n");
#endif

3. Macro implementation functions

Swap two numbers:

#define swap(a,b)     ((a)=(a)+(b);(b)=(a)-(b);(a)=(a)-(b))

Compare two numbers and return the smallest number:

#define MIN(a,b)     ((a)<(b)?(a):(b))

Find the number of array elements:

#define LENGTH(array)    (sizeof(array) / sizeof(array[0]))

4. Cross-line macro definition

#define    SWAP(a,b)    do { \
                        int t = 0;\
                        t = a; \
                        a = b; \
                        b = t; \
                    } while(0)

Careful you may have noticed that the above macros do not have a semicolon ";" at the end of

5. Prevent header files from being repeatedly included

#ifndef __TEST_H 
#define __TEST_H 
    // Header file content#endif

6. The difference between parameter macro and parameter function

  • 1. The macro will be replaced simply when the compiler compiles the source code, and will not perform any logical detection, that is, simple code copying;
  • 2. The type of parameter is not considered when defining the macro;
  • 3. The use of parameter macros will cause multiple copies of the code block with the same function in the target file, which will increase the size of the target file;
  • 4. The running speed of parameter macros will be faster than that of functions, because there is no need for parameter stack pressing/output operations;
  • 5. Be more careful when defining parameter macros and add more brackets;
  • 6. The function only exists in the target file, which saves program space more;
  • 7. The call of a function will involve the passing of parameters, and the stack pressing/output operation is relatively slow;
  • 8. There is a problem with passing values ​​and address (pointers) in the function parameters, and the parameter macro does not exist;

7. Things to note

  • 1. The macro definition will not replace the parameters in quotes;
  • 2. To replace, you can prefix the parameter with '#' and convert it into a "string", such as:
#define dprint(expr) printf(#expr " = %d\n", expr)

Use dprint(x/y) and it is replaced with:

printf("x/y" " = %g\n", x/y);

Right now

printf("x/y = %g\n", x/y);

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.