Simple explanation of the definition and use of macro in C language

Source: Internet
Author: User
Tags mul

A macro definition is one of the pre-compilation features, pre-compilation, also known as preprocessing, is the preparatory work for the compilation phase. The instructions that handle the beginning of the #, such as the file code that the copy #include contains, #define宏定义的替换, conditional compilation, and so on.
Benefits of using macro definitions: Benefits of using Macro definitions: Improves program versatility and readability, reduces inconsistencies, reduces input errors, and facilitates modification. For example π is a constant that we sometimes use in multiple places of the program, and if each use is redefined, one is more troublesome and error prone, so we can use π as a macro definition.

Syntax Description:
(1) Macro names are generally capitalized
(2) The use of macros can improve the universality and readability of the program, reduce inconsistencies, reduce input errors and facilitate modification. Example: array size common macro definitions
(3) Preprocessing is the processing before compiling, and one of the tasks of compiling is grammar checking, preprocessing does not do grammar checking.
(4) No semicolon at the end of the macro definition;
(5) The macro definition is written outside the curly braces of the function, and the scope is followed by the program, usually at the very beginning of the file.
(6) The scope of the macro definition can be terminated with the #undef command
(7) macro definition can be nested
(8) The string "" never contains a macro
(9) Macro definition does not allocate memory, variable definition allocates memory.

Defined:

?
12345 #define 宏名 内容#define kArrLen 10 // OC驼峰命名法:首字符为k,其他单词首字符大写#define ARR_LEN 10 // C语言中的宏命名规范:所有字母大写,单词用下划线分隔

For example:

?
1 #define PI 3.1415926

We can use PI instead of 3.1415926:

?
123 floatr = 0.5;floatarea = PI * r * r;printf("area = %f\n", area);

The PI is replaced with 3.1415926 during the program precompilation phase:

?
1 floatarea = 3.1415926 * r * r;

The macro definition is simple and well understood, but there are a few caveats:

1, because the macro definition is simply a replacement, if the macro definition of the contents of the operation, you want to put each variable parentheses, so as not to affect the priority of the operation

For example: #define MUL (A, B) (A * b)

How to use:

?
1 MUL(3 + 5, 5 + 6) //会替换成 3 + 5 * 5 + 6 = 34

We want (3 + 5) * (5 + 6), so when defining a macro, you should:

?
1 #define MUL(A, B) ((A) * (B))

2. Note that there is no semicolon (;) Behind the macro definition, it is not a statement

3, the macro definition is to be replaced in the early stage of the program compilation, the program has not been compiled

Simple explanation of the definition and use of macro in C language

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.