Use of macro definition

Source: Internet
Author: User

Macros in C are divided into two types: object-like macro and function-like macro ).

Predefined macros:

1. The behavior of the predefined macro is specified by the compiler.

2. The front and back parallel bars are generally pre-defined.

2. _ COUNTER _ is a predefined macro. This value will be counted from 0 during compilation, and 1 will be added each time it is called.

3. _ FILE _ returns the absolute path of the current FILE.

4. _ LINE _ returns the number of rows in the file when the macro is expanded.

5. _ func _ indicates the function name of the scope where the macro is located.

6. _ VA_ARGS _ indicates all the remaining parameters in... in macro definition.

7. Output rect, size, and point macros:

 
  1. #define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", #rect, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
  2. #define NSLogSize(size) NSLog(@"%s w:%.4f, h:%.4f", #size, size.width, size.height)
  3. #define NSLogPoint(point) NSLog(@"%s x:%.4f, y:%.4f", #point, point.x, point.y)

8. Use ({...}) format. This type of statement can be similar to many scripting languages. After sequential execution, the value of the last expression is returned. For example, after the following code is executed, the value of a is 3, and B and c only exist in the code domain limited by braces:

 
  1. int a = ({
  2. int b = 1;
  3. int c = 2;
  4. b + c;
  5. });
  6. // => a is 3
9. # Is a special symbol in a macro. It indicates connecting two parameters. 10. # the function of a single well number is to convert it into a C string by adding "" to the two ends after replacement.

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.