Summary of usage of system macros in OBJECTIVE-C

Source: Internet
Author: User

Let's talk about what we'll mention in this article: ##,__va_args__, __file__, __line__, __function__, etc.
Macro variables:
To give an example, you'll use these macros:

    #define myprintf (...) printk ("[Lch]:file:%s, line:%d, function:%s," \           __va_args__, __file__, __ line__, __function__);  

The purpose of the #define here is to replace myprintf () with the contents of the following large string, in parentheses ... The contents of the __va_args__ are copied in the same place. The final output is as follows:

[Lch]:file:arch/arm/mach-omap2/board-omap3wscec-camera.c, line:163, Function:beagle_cam_init,camera Init !

Analytical:

1) __va_args__: In general, will be the left macro ... The contents of the __va_args__ are copied in the right place. It is a mutable parameter of the macro, is new in the C99 specification, and currently appears to be only GCC support (VC starting from VC2005 support). Note that the output format of printf is a string to the left of the parentheses, the right is a variable, and the right variable corresponds to the left output format of one by one. So in the example above, __va_args__ can only be a string constant that does not contain any variables. Because the above example if __va_args__ contains variables, the output of the whole printf and the variable can not be matched, the output will be wrong.

If you are simply replacing a function name, there are no special requirements for __va_args__ at this time: #define MYPRINTF (...) printk (__va_args__), which can be used when debugging a program:

    #ifndef log_ndebug_function      #define logfunc (...) ((void) 0)      #else      #define Logfunc (...) (PRINTK (__va_args__))      #endif  

2) __FILE__: Macro will replace the current source file name at precompilation
3) __LINE__: Macro will replace the current line number at precompilation
4) __FUNCTION__: Macro will replace the current function name at precompilation
5) Similar macro and __time__,__stdc__, __timestamp__, etc., it is completely when a variable to use.

Macro Connector # #:
For example: a macro is defined as a # define XNAME (n) x# #n, the code is: XNAME (4), then at precompilation, the macro discovers that XNAME (4) matches XNAME (n), then n is 4, then the contents of N to the right are also 4, then the entire XNAME (4) is replaced with X # #n, i.e. X4, so the end result is XNAME (4) to X4.
The code is as follows:

#include <stdio.h>#defineXNAME (n) x # # N#definePRINT_XN (n) printf ("x" #n "=%d/n", x # # N);intMainvoid)      {      intXNAME (1) = -;//becomes int x1 = 14;     intXNAME (2) = -;//becomes int x2 = 20; PRINT_XN (1);//becomes printf ("x1 =%d,", X1); PRINT_XN (2);//becomes printf ("x2 =%d/n", x2);     return 0; }  

Output is: x1 = +, x2 = 20

Summary of usage of system macros in OBJECTIVE-C

Related Article

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.