A brief explanation of the definition and use of _c language in C language

Source: Internet
Author: User
Tags mul naming convention

Macro definition is a precompiled function, precompilation is also known as preprocessing, is the preparatory work for the compilation of the stage. Handle the instructions at the beginning of the #, such as copy #include contained file code, #define宏定义的替换, conditional compilation, etc.
Benefits of using macro definitions: The benefits of using macro definitions can improve the versatility and readability of programs, reduce inconsistencies, reduce input errors, and facilitate modification. For example π this constant, we sometimes use in many parts of the program, if each use is redefined, one of the more troublesome, and then error prone, so we can make the macro definition of pi to use.

Syntax Description:
(1) Macro name generally used in uppercase
(2) The use of macros can improve the universality and readability of the program, reduce the inconsistency, reduce the input error and facilitate modification. For example: array size common macro definition
(3) Preprocessing is the processing before compiling, and one of the tasks of compiling work is grammar check, preprocessing does not do grammar check.
(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 the program that follows, usually at the very beginning of the file.
(6) The scope of the macro definition can be terminated with the #undef command
(7) Macro definitions can be nested
(8) The string "" will never contain macros
(9) Macro definition does not allocate memory, variable definition allocates memory.

Defined:

#define Macro Name

#define KARRLEN//OC Hump nomenclature: The first character is K, the first character of other words is capitalized

#define ARR_LEN//C language of the macro naming convention: All uppercase letters, words are separated by underscores

For example:

#define PI 3.1415926

We can use PI instead of 3.1415926:

float r = 0.5;
float area = PI * R * r;
printf ("area =%f\n", area);

PI is replaced with 3.1415926 in the program precompilation phase:

float area = 3.1415926 * R * r;

The macro definition content is simple and understandable, but there are a few things to note:

1, because the macro definition is just a simple substitution, if the macro definition of the content of the time, to add each variable parentheses, so as not to affect the priority of operations

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

How to use:

MUL (3 + 5, 5 + 6)//will be replaced with 3 + 5 * 5 + 6 = 34

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

#define MUL (A, B) ((a) * (b))

2, note that the macro definition is not followed by a semicolon (;), it is not a statement

3, the macro definition is in the early stages of compiling the program to replace, at this time the program has not yet compiled

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.