Advanced usage of macros-##,__ VA_ARGS __, _ FILE __, _ FUNCTION _, etc.

Source: Internet
Author: User

First, let's talk about the following content: ##,__ VA_ARGS __, _ FILE __, _ LINE _, _ FUNCTION _, etc.
Macro variable:
For example, the above macros are used:

#define myprintf(...) printk("[lch]:File:%s, Line:%d, Function:%s," \     __VA_ARGS__, __FILE__, __LINE__ ,__FUNCTION__);

Here # define is used to replace myprintf () with the next large string of content, while the content of... in brackets is copied to the position of _ VA_ARGS _ as is. The final output is as follows:

[Lch]: File: arch/arm/mach-omap2/board-omap3wscec-camera.c, Line: 163, Function: beagle_cam_init, camera init!

Resolution:

1) _ VA_ARGS __: in general, the content of... in the left macro is copied to the position of _ VA_ARGS _ on the right. It is a variable parameter macro and is newly added in the new C99 specification. Currently, it seems that only gcc is supported (VC is supported since VC2005 ). Note that the output format of printf is a string on the left side of the brackets and a variable on the right side. The right variables correspond to the left output format one by one. In the above example, _ VA_ARGS _ can only be a String constant without any variables. In the above example, if _ VA_ARGS _ contains variables, the output of the entire printf cannot correspond to the variables one by one, and the output will fail.

If you only replace the function name, you can use the following method. In this case, _ VA_ARGS _ has no special requirements: # define myprintf (...) printk (_ VA_ARGS _), which can be used in program debugging as follows:

#ifndef LOG_NDEBUG_FUNCTION#define LOGFUNC(...) ((void)0)#else#define LOGFUNC(...) (printk(__VA_ARGS__))#endif

2) _ FILE _: The Macro will be replaced with the current source FILE name during pre-compilation.
3) _ LINE __: the Macro will be replaced with the current row number during pre-compilation.
4) _ FUNCTION __: the macro is replaced with the current FUNCTION name during pre-compilation.
5) Similar macros include _ TIME __,__ STDC __, _ TIMESTAMP _, etc., which can be used as a variable.

Macro connector ##:
For example, if the macro is defined as # define XNAME (n) x # n and the code is XNAME (4), then XNAME (4) is displayed during pre-compilation) match XNAME (n), set n to 4, change n content on the right to 4, and replace the entire XNAME (4) with x # n, XNAME (4) is changed to x4.
The Code is as follows:

#include <stdio.h>#define XNAME(n) x ## n#define PRINT_XN(n) printf("x" #n " = %d/n", x ## n);int main(void){int XNAME(1) = 14; // becomes int x1 = 14;int XNAME(2) = 20; // becomes int x2 = 20;PRINT_XN(1); // becomes printf("x1 = %d,", x1);PRINT_XN(2); // becomes printf("x2 = %d/n", x2);return 0;}

Output: x1 = 14, x2 = 20

PS: compilation process:
1. Scan the parsing File
2. Preprocessing (if the macro is processed at this time, the replaced text will be replaced)
3. Compile the processed source code and output the code of the assembly language (the control process of the C language is processed)
4. compile it as a binary target file
5. Link to the library to output the final program file
(Processing and execution of macros and C languages at different stages)

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.