Macros in C language 2

Source: Internet
Author: User
The original object-like macro

The object-like macro is a simple identifier.Code. Because it looks like a data field used in code, it is called an object-like macro. The most common use of this type of macro is to replace a numeric constant with a specified symbol.

Use the "# define" command to define a macro. # define is followed by a macro name and a macro content. The Compiler replaces the macro name with the macro content during preprocessing. For example,

# Define buffer_size 1024

A macro named buffer_size is defined. buffer_size refers to the number 1024. If buffer_size appears somewhere after the # define statement

Foo = (char *) malloc (buffer_size );

The Preprocessor can recognize that buffer_size is a macro and replace it with 1024. In the compiler's opinion, the above sentence is the same as the following one.

Foo = (char *) malloc (1024 );

In some conventional rules, macro names are generally capitalized. When all the symbols that indicate macros are capitalized, it is easier to read the code.

The macro content ends at the end of the # define row. Of course, if you must write a macro definition into multiple rows, there are also some methods, that is, escape the carriage return character with a backslash. When a macro is expanded, the escaped carriage return will become the same line, for example

# Define numbers 1, \ 2, \ 3 int X [] = {numbers}; ==> int X [] = {1, 2, 3 };
 
This method is most often used in the row number representation in the error message.
 
Macro definition has no mandatory constraints. Parentheses do not have to be paired, nor do you require that your macro be a valid C statement after expansion.
 
C LanguageThe macro definition takes effect only after you write it. Therefore, the following code is entered into the pre-processor:
Foo = x; # define x 4 bar = X;
 
Generate the following code:
Foo = x; bar = 4;
 
When a Preprocessor extends a macro, the macro extension is recursive until there is no symbol to replace. For example,
# Define tablesize bufsize # define bufsize 1024 tablesize ==> bufsize ==> 1024
 
Tablesize is expanded to bufsize, and bufsize is expanded to 1024.
Note that bufsize is defined after tablesize, so after tablesize is expanded, it is the bufsize, instead of checking whether the bufsize can be expanded. The pre-processor will not continue scanning for bufsize until it sees the bufsize symbol.
 
If you change the definition of bufsize in the same location of the Code, the situation may seem to be in violation of the intuitive:
# Define bufsize 1020 # define tablesize bufsize # UNDEF bufsize # define bufsize 37
 
Tablesize is expanded to 37, because tablesize is first expanded to bufsize and then expanded to 37.
 
If a macro contains the macro itself after its launch, whether it is directly included or indirectly included, the macro launch will immediately stop. This rule prevents infinite recursion. For more details, see self-contained macros.

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.