"C-language summary" macro definition, preprocessing

Source: Internet
Author: User

Macro definition Simple macro

Format: #define Identifier substitution list

#define N 100int a[N];//N就是100
Macros with parameters

Format: #define identifier (x1, x2, x3,...... xn) Replace list

#define MAX(x, y) ((x) > (y) ? (x) : (y))

Note: When the program compiles, it will replace the uppercase identifier with the original expression, so the program will increase at compile time.

Advantages or Disadvantages

Advantages
① program may be a little faster
② Macros are more versatile
Disadvantages
① compiled code usually gets bigger
② cannot point to a macro with a pointer
③ macro may calculate its parameters more than once

Use of parentheses in macro definitions

#define TWO_PI (2 * 3.1415)
#define RES(x) ((x) * 10)
Cause: Because the macro is a substitution expression at compile time, many uncertainties
For example:

#define RES(x) (x * 10) //假如宏是这样定义的,参数没有括号int1);int110//编译器会替换成这样。与结果完全不对
Cancel macro

Format: #define N

Conditional compilation
#if 常量表达式    编译内容#endif
defined运算法仅用于预处理器,N必须是定义的宏#if defined(N)....#endif

The IFDEF directive tests whether an identifier has been defined as a macro

#ifdeft 标示符......#endif
#ifndef 标示符......#endif

"C-language summary" macro definition, preprocessing

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.