Macro Definition Learning

Source: Internet
Author: User

Macro Definition Learning

(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 cannot be nested
(8) The string "" never contains a macro
(9) Macro definition does not allocate memory, variable definition allocates memory.
(10) The macro definition does not have a type problem, and its parameters are untyped.

1. The reason that the macro definition must be bracketed is that it is completely replaced by the macro definition. If no parentheses may cause the operation of the following main function to be replaced after substitution is not what we want to express the original intent such as the following code: # include <stdio.h> #define YEAR 365+2//If we do not add brackets (365+ 2) Then in the following call process will occur in the source code problem, will first calculate 365+2*2 and we would like to express the int main (void)//Is (365+2) *2{int Aa=year*2;printf ("%d", a);return 0;} 2. A macro definition indicates how many seconds are in a year: By default, a macro defines a data type that is shaped (that is, an int), so you should be aware of the scope of his type when considering the macro definition. If you exceed the default shaping type, you need to force the type conversion, such as the following example: Let's define the time of year Define year () UL behind the UL is forced type conversion.

Macro Definition Learning

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.